C++ C++;在快速排序函数中获取StackOverflow错误

C++ C++;在快速排序函数中获取StackOverflow错误,c++,algorithm,sorting,quicksort,C++,Algorithm,Sorting,Quicksort,当我试图使用快速排序对一个大数组进行排序时,我遇到了stackoverflow错误,这个数组是按降序排列的。我想使用下面的代码按升序排序: int partition_lastElementPivot(int * arr, int lo, int hi) { int x = arr[hi]; int i = lo - 1; for (int j = lo; j < hi; j++) { if (arr[j] <= x)

当我试图使用快速排序对一个大数组进行排序时,我遇到了stackoverflow错误,这个数组是按降序排列的。我想使用下面的代码按升序排序:

int partition_lastElementPivot(int * arr, int lo, int hi)
{
    int x = arr[hi];
    int i = lo - 1;
    for (int j = lo; j < hi; j++)
    {
        if (arr[j] <= x)
        {
            i++;
            int temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }
    }
    int temp = arr[hi];
    arr[hi] = arr[i + 1];
    arr[i + 1] = temp;

    return i + 1;
}

void quicksortLastElementPivot(int*arr, int lo, int hi)
{
    if (lo<hi)
    {
        int mid = partition_lastElementPivot(arr, lo, hi);
        quicksortLastElementPivot(arr, lo, mid - 1);
        quicksortLastElementPivot(arr, mid + 1, hi);
    }
}
int分区\u lastElementPivot(int*arr,int-lo,int-hi)
{
int x=arr[hi];
int i=lo-1;
对于(int j=lo;jif(arr[j]正如您在这里发现的那样,Quicksort在最坏情况下的性能非常糟糕。它在堆栈深度为5000的情况下调用自己。对此进行了很好的讨论。特别是,它提到了解决堆栈溢出问题的方法

简单地说,这意味着不是最后一次调用
quicksortLastElementPivot
,然后立即返回,而是循环回到函数的开头。这具有相同的效果,但尾部递归不会增加堆栈大小。要使其起作用,必须确保对两个分区中较小的分区进行排序首先,使用传统的递归,较大的分区按尾部递归排序

void quicksortLastElementPivot(int*arr、int-lo、int-hi)
{
重复:

if(loC++标准未定义可执行程序的堆栈大小

此限制通常在项目的生成文件或链接器命令文件中定义

根据您的IDE,您也可以在项目设置中找到它(在链接器配置下)

TonyK给出的答案很好地解释了最坏情况下快速排序的堆栈用法(这正是代码中的情况,
arr
按相反顺序排序)。

包括
使用名称空间std;
无效快速排序(int*arr、int左、int右)
{
int i=左;
int j=右;
int pivot=arr[rand()%(右-左)+左];
而(ipivot)
{
j--;
}
如果(i>n);
int*arr=新的int[n];
对于(int i=0;i>arr[i];
}
快速排序(arr,0,n-1);
对于(int i=0;iCUT是的C++限制了堆栈大小。也可以看到关于QuoS排序和枢轴和堆栈空间的讨论。谢谢答案。
int arr[5000];
int count = 5001;
for(int i=0; i<5000; i++)
{
    arr[i] = count;
    count--;

}
quicksortLastElementPivot(arr, 0, 4999)
void quicksortLastElementPivot(int*arr, int lo, int hi)
{
TailRecurse:
    if (lo<hi)
    {
        int mid = partition_lastElementPivot(arr, lo, hi);
        if (mid < (lo + hi) / 2)
            { // First partition is smaller
            quicksortLastElementPivot(arr, lo, mid - 1); // Sort first partition
            lo = mid + 1; goto TailRecurse;              // Sort second partition
            }
        else
            { // Second partition is smaller
            quicksortLastElementPivot(arr, mid + 1, hi); // Sort second partition
            hi = mid - 1; goto TailRecurse;              // Sort first partition
            }
    }
}
#include <iostream>
using namespace std;

void QuickSort(int *arr, int left, int right)
{
int i = left;
int j = right;
int pivot = arr[rand() % (right - left) + left];
while (i < j)
{
    while (arr[i] < pivot)
    {
        i++;
    }
    while (arr[j] > pivot)
    {
        j--;
    }
    if (i <= j)
    {
        swap(arr[i], arr[j]);
        i++;
        j--;
    }
}
if (left < j)
{
    QuickSort(arr, left, j);
}
if (i < right)
{
    QuickSort(arr, i, right);
}
  }
  int main()
 {
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
int n;
cin >> n;
int *arr = new int[n];

for (int i = 0; i < n; i++)
{
    cin >> arr[i];
}
QuickSort(arr, 0, n - 1);
for (int i = 0; i < n; i++)
{
    cout << arr[i] << " ";
}

delete arr;
}