对数组排序会给出错误的值 我在C++中实现了快速排序。下面是我的代码 #include <iostream> using namespace std; template <typename T> void swap(T *a, T *b) { T temp; temp = *a; *a = *b; *b = temp; } template <typename T> void PrintArray(T arr[], int n) { cout << "---------- Array ----------" << endl; for (int i=0; i<n ; i++) { cout << arr[i] <<'\t'; } cout << endl; } template <typename T> int partition(T arr[], int low, int high) { T pivot = arr[low]; int i = low+1, j = high; do { while (pivot >= arr[i]) { i += 1; } while (pivot < arr[j]) { j -= 1; } if (i<j) { swap<T>(arr[i], arr[j]); } }while( i < j); swap<T>(arr[low], arr[j]); return j; } template <typename T> void quick_sort(T arr[], int low, int high) { if (low < high) { int parition_index; parition_index = partition<T>(arr, low, high); quick_sort<T>(arr, low, parition_index-1); quick_sort<T>(arr, parition_index+1, high); } } int main() { // Array creation int n = 8; int a[] ={4, 3,2, 1, 18, -1, 89, -200}; // Array sorting quick_sort<int>(a,0, n); PrintArray<int>(a, n); return 0; } #包括 使用名称空间std; 模板 无效掉期(T*a、T*b) { 温度; 温度=*a; *a=*b; *b=温度; } 模板 无效打印数组(T arr[],int n) { cout

对数组排序会给出错误的值 我在C++中实现了快速排序。下面是我的代码 #include <iostream> using namespace std; template <typename T> void swap(T *a, T *b) { T temp; temp = *a; *a = *b; *b = temp; } template <typename T> void PrintArray(T arr[], int n) { cout << "---------- Array ----------" << endl; for (int i=0; i<n ; i++) { cout << arr[i] <<'\t'; } cout << endl; } template <typename T> int partition(T arr[], int low, int high) { T pivot = arr[low]; int i = low+1, j = high; do { while (pivot >= arr[i]) { i += 1; } while (pivot < arr[j]) { j -= 1; } if (i<j) { swap<T>(arr[i], arr[j]); } }while( i < j); swap<T>(arr[low], arr[j]); return j; } template <typename T> void quick_sort(T arr[], int low, int high) { if (low < high) { int parition_index; parition_index = partition<T>(arr, low, high); quick_sort<T>(arr, low, parition_index-1); quick_sort<T>(arr, parition_index+1, high); } } int main() { // Array creation int n = 8; int a[] ={4, 3,2, 1, 18, -1, 89, -200}; // Array sorting quick_sort<int>(a,0, n); PrintArray<int>(a, n); return 0; } #包括 使用名称空间std; 模板 无效掉期(T*a、T*b) { 温度; 温度=*a; *a=*b; *b=温度; } 模板 无效打印数组(T arr[],int n) { cout,c++,algorithm,sorting,quicksort,C++,Algorithm,Sorting,Quicksort,@FrançoisAndrieux的评论对于发现问题非常有用 正如他指出的那样,j将8作为超出范围的值。 解决问题 步骤1:int main()中的quick_sort(a,0,n-1); 步骤2:取消自定义的swap函数你应该知道你的swap函数没有被使用。如果这是编译的,那是因为正在拉入std::swap并且你的使用命名空间std;允许非限定的swap调用使用std::swap>。您的swap函数需要指针。使用。无需重新发明轮子。通过调试程序,第一次调用partition时,j是8。这会导致

@FrançoisAndrieux的评论对于发现问题非常有用

正如他指出的那样,
j
将8作为超出范围的值。 解决问题

步骤1:
int main()中的
quick_sort(a,0,n-1);


步骤2:取消自定义的
swap
函数

你应该知道你的
swap
函数没有被使用。如果这是编译的,那是因为
正在拉入
std::swap
并且你的
使用命名空间std;
允许非限定的
swap
调用使用
std::swap
>。您的
swap
函数需要指针。使用。无需重新发明轮子。通过调试程序,第一次调用
partition
时,
j
8
。这会导致您使用
arr[8]进行交换
这是超出范围和未定义的行为。你从gfg复制的代码是垃圾。我不想说别人的工作不好,但在那里看到垃圾是很常见的,许多初学者复制他们的代码并对这些问题感到惊讶。这没有帮助。对数组的所有更改都有一行,特别是
swap(arr[i],arr[j]);
。这样可以很容易地添加诊断,以帮助您发现哪里出了问题。(调试器对于每次执行时可重复的内容非常有用;对于仅在某些时间发生的事情,某种类型的日志记录可能更方便。)尝试一下简化版的代码。我使用最右边的元素作为轴心。