Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.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# 对15000+;数组排序时获取堆栈溢出异常;元素_C#_Recursion_Exception_Stack Overflow_Quicksort - Fatal编程技术网

C# 对15000+;数组排序时获取堆栈溢出异常;元素

C# 对15000+;数组排序时获取堆栈溢出异常;元素,c#,recursion,exception,stack-overflow,quicksort,C#,Recursion,Exception,Stack Overflow,Quicksort,这是我的快速排序算法实现。我在尝试对大于15k元素的数组进行排序时抛出了System.StackOverflowException,并显示消息“类型异常”System.StackOverflowException。“。我实际上检查了15000、19000、20000、30000个元素,最后3个输入抛出了异常 private static int ArraySplitter(int[] intArr, int low, int high) { int pivot = intArr[high

这是我的快速排序算法实现。我在尝试对大于15k元素的数组进行排序时抛出了
System.StackOverflowException
,并显示消息“类型异常”System.StackOverflowException。“。我实际上检查了15000、19000、20000、30000个元素,最后3个输入抛出了异常

private static int ArraySplitter(int[] intArr, int low, int high)
{
    int pivot = intArr[high];
    int lowIndex = (low - 1);
    for (int i = low; i < high; i++)
    {
        if (intArr[i] <= pivot)
        {
            lowIndex++;
            int temp = intArr[lowIndex];
            intArr[lowIndex] = intArr[i];
            intArr[i] = temp;
        }
    }
    int tempHigh = intArr[lowIndex + 1];
    intArr[lowIndex + 1] = intArr[high];
    intArr[high] = tempHigh;
    return lowIndex + 1;
}
private static void QSort(int[] intArr, int low, int high)
{
    if (low < high)
    {
        int index = ArraySplitter(intArr, low, high);
        QSort(intArr, low, index - 1);
        QSort(intArr, index + 1, high);
    }
}
public static void QuickSort(int[] intArr)
{
    QSort(intArr, 0, intArr.Length - 1);
}  
private静态int-ArraySplitter(int[]intar,int-low,int-high)
{
int pivot=intArr[高];
int低指数=(低-1);
对于(int i=低;i<高;i++)
{

if(intArr[i]如果数据已经排序或反向排序,则为pivot选择子数组中的第一个或最后一个元素会导致最坏情况下的空间复杂度O(n)。对于问题代码,请将中间元素与最后一个元素(数组[高])交换在分割之前处理排序或反向排序的数据。仍然有其他模式会导致最坏的情况

仅对较小的分区使用递归将堆栈空间复杂度限制为O(log(n)),但最坏情况下的时间复杂度仍然为O(n^2)

private static void QSort(int[]intar,int low,int high)
{
while(低<高)
{
int索引=阵列分裂器(intArr、低、高);

如果((index-low)它能成功地对一个包含10个元素的数组进行排序吗?是的,它能。如果在这种情况下,它工作得很好。我将假设代码是有效的,调用堆栈确实耗尽了空间。我建议以非递归方式编写此代码。另外,请查看并注意警告。@John QuickSort(或任何其他“一分为二”排序)不能真正在30K个项目上崩溃-大约是15个递归级别-这远远低于ASP.Net堆栈跟踪的平均值…从理论上讲,使用非随机分区选择器为快速排序设置数据是可能的,但我怀疑OP是否能想出一个。这里有一个类似的问题:
def QUICKSORT(arr, p, r):
    if p < r:
        q = PARTITION(arr, p, r)
        QUICKSORT(arr, p, q-1)
        QUICKSORT(arr, q+1, r)


def PARTITION(arr, p, r):
    x = arr[r]
    i = p-1
    for j in range(p, r-1):
        if arr[j] <= x:
            i = i + 1
            temp = arr[i]
            arr[i] = arr[j]
            arr[j] = temp
    temp = arr[i+1]
    arr[i+1] = arr[r]
    arr[r] = temp
    return i+1
private static void QSort(int[] intArr, int low, int high)
{
    while (low < high)
    {
        int index = ArraySplitter(intArr, low, high);
        if((index - low) <= (high - index)){
            QSort(intArr, low, index - 1);
            low = index + 1;
        } else {
            QSort(intArr, index + 1, high);
            high = index - 1;
        }
    }
}