找到你要的答案

Q:Stack OverFlow Exception in Quicksort Algorithm

Q:在快速排序算法,堆栈溢出异常

I am having a problem with my quicksort algorithm when trying to sort relatively big list that has no duplicates and are almost sorted (with only 5 numbers to sort).

Below please find the code.

 public static void MyQuickSort(List<int> list, int left, int right)
    {
        if (list == null || list.Count <= 1)
            return;

        if (left < right)
        {
            int pivotIdx = MyPartition(list, left, right);
            //Console.WriteLine("MQS " + left + " " + right);
            //DumpList(list);
            MyQuickSort(list, left, pivotIdx - 1);
            MyQuickSort(list, pivotIdx, right);
        }
    }

static int MyPartition(List<int> list, int left, int right)
    {
        int start = left;
        int pivot = list[start];
        left++;
        right--;

        while (true)
        {
            while (left <= right && list[left] <= pivot)
                left++;

            while (left <= right && list[right] > pivot)
                right--;

            if (left > right)
            {
                list[start] = list[left - 1];
                list[left - 1] = pivot;

                return left;
            }


            int temp = list[left];
            list[left] = list[right];
            list[right] = temp;

        }
    }

我有一个问题与我的快速排序算法在排序比较大名单没有重复的、几乎是排序(只有5个数字排序)。

下面请查找代码。

 public static void MyQuickSort(List<int> list, int left, int right)
    {
        if (list == null || list.Count <= 1)
            return;

        if (left < right)
        {
            int pivotIdx = MyPartition(list, left, right);
            //Console.WriteLine("MQS " + left + " " + right);
            //DumpList(list);
            MyQuickSort(list, left, pivotIdx - 1);
            MyQuickSort(list, pivotIdx, right);
        }
    }

static int MyPartition(List<int> list, int left, int right)
    {
        int start = left;
        int pivot = list[start];
        left++;
        right--;

        while (true)
        {
            while (left <= right && list[left] <= pivot)
                left++;

            while (left <= right && list[right] > pivot)
                right--;

            if (left > right)
            {
                list[start] = list[left - 1];
                list[left - 1] = pivot;

                return left;
            }


            int temp = list[left];
            list[left] = list[right];
            list[right] = temp;

        }
    }
answer1: 回答1:

Recursive algorithms are fun in academic exercises, but they're rarely used in practice, because of what you're seeing: too many recursive calls leads to excessive call stack usage, eventually terminating due to a stack overflow.

Short answer: try your code with a smaller data set.

Long answer: re-write your code to avoid recursion. You can use your own stack instead of the program's call stack.

递归算法是有趣的学术练习,但他们很少在实践中使用,因为你所看到的:太多的递归调用导致过多的调用堆栈的使用,最终终止由于堆栈溢出。

使用一个较小的数据集尝试代码。

长回答:重新编写代码避免递归。您可以使用自己的堆栈而不是程序的调用堆栈。

c#  algorithm  sorting  exception  stack