Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++快速排序继续崩溃_C++_Algorithm_Runtime Error_Quicksort - Fatal编程技术网

C++快速排序继续崩溃

C++快速排序继续崩溃,c++,algorithm,runtime-error,quicksort,C++,Algorithm,Runtime Error,Quicksort,我已经实施了一段时间的快速排序分析,它可以编译,但在运行后继续崩溃,我不知道为什么。另外,我认为helper函数的其中一个返回语句ifhigh==low中有一个错误,我不知道这是否导致了我的问题?代码如下,它接收一个由40个随机生成的整数组成的数组 int partition(int A[], int low, int high) { //Partition the array into two parts //Pre Condition: A[] must have more th

我已经实施了一段时间的快速排序分析,它可以编译,但在运行后继续崩溃,我不知道为什么。另外,我认为helper函数的其中一个返回语句ifhigh==low中有一个错误,我不知道这是否导致了我的问题?代码如下,它接收一个由40个随机生成的整数组成的数组

int partition(int A[], int low, int high) {
   //Partition the array into two parts
   //Pre Condition: A[] must have more than 2 elements
   int mid = (low + high) / 2;
   int pivot = A[mid]; //Store the value at A[mid] in our pivot
   A[mid] = A[low]; //Make a "gap" at low
   while(low < high) {
      //The "gap" is currently at low
      while(low < high && A[high] >= pivot) {
         high--;
      }
      A[low] = A[high]; //"Gap" is now at A[high]
      while(low < high && A[low] <= pivot) {
         low++;
      }
      A[high] = A[low];
      A[low] = pivot;
   }
   return low;
}

void quickSort(int A[], int low, int high) {
   //QuickSort Helper Function
   //Pre Condition: A > 0
   //Post Condition: Partition of the array
   if(low == high) //***ERROR HERE***
      return;
   if(low + 1 == high) {
      if(A[low] > A[high]) {
         swap(A, high, low);
      }
      return;
   }
   //Assign the return value to pivotPos after running partition method
   int pivotPos = partition(A, low, high); 
   quickSort(A, low, pivotPos - 1); //Recursive call
   quickSort(A, pivotPos + 1, high); //Recursive call
   return;
}

void quickSort(int A[], int n) {
   //Quicksort sorting algorithm
   //Precondition: A < 0
   //Postcondition: Sorted array in ascending order
   quickSort(A, 0, n - 1); //call our helper function
}

你说的“撞车”是什么意思?您收到了什么错误消息?调试器怎么说?我用的是Scite我不相信它有调试器?我得到一个Windows错误,好像程序处于无限循环中。不是Scite中的错误,而是实际的Windows.exe错误错误错误是什么?为什么不在调试器中使用此代码来调试正在查找的其他错误有免费的调试器可供使用。经过几次迭代后,low变为3,high变为2,递归调用partition,直到堆栈空间和segfault用完,才有任何进展。因此,我应该将第一个条件更改为>=