如何解决此快速排序?

时间:2018-08-17 07:56:25

标签: c# quicksort

我已经在c#中创建了一个快速排序算法,该算法在数组中只有10个项目时可以使用。当我增加这个数字时,它陷入了无限循环。这是问题所在的代码:

         while (true)
         {
            while (((IComparable)arrayToSort[left]).CompareTo(pivot) < 0)
            {
                left++;
            }

            while (((IComparable)arrayToSort[right]).CompareTo(pivot) > 0)
            {
                right--;
            }

            if (left < right) //This is where the loop becomes infinite.
            {
                object temp =arrayToSort[right];
                arrayToSort[right] = arrayToSort[left];
                arrayToSort[left] = temp;
                reDrawer.reDrawSample(right, g, arrayToSort, picSamples); //This is used to draw lines that are sorted to make the sorting visual.
                reDrawer.reDrawSample(left, g, arrayToSort, picSamples);
                refresher.refreshPicture(picSamples); //This is used to refresh the image with the lines.
                Thread.Sleep(20);
            }
            else
            {
                return right;
            }

while语句的比较都是错误的,但是如果为true,我看不出有什么办法。当右==枢轴或左==枢轴时会发生这种情况。

任何人都可以看到问题吗?

该数组当前有50个变量,并且此问题仅在大量变量时发生。我不想使用少于50个变量的数组。

这是完整的方法:

class Quick_Sort
{
    /// <summary>
    /// This subroutine creates a pivot and partitions the array accordingly.
    /// </summary>
    /// <param name="arrayToSort"></param>
    /// <param name="left"></param>
    /// <param name="right"></param>
    /// <returns></returns>
    public int partition(ArrayList arrayToSort, int left, int right)
    {
        int pivot = (int)arrayToSort[left];
        ReDrawer reDrawer = new ReDrawer();
        Refresher refresher = new Refresher();

        while (true)
        {
            while (((IComparable)arrayToSort[left]).CompareTo(pivot) < 0)
            {
                left++;
            }

            while (((IComparable)arrayToSort[right]).CompareTo(pivot) > 0)
            {
                right--;
            }

            if (left < right)
            {
                object temp =arrayToSort[right];
                arrayToSort[right] = arrayToSort[left];
                arrayToSort[left] = temp;
                reDrawer.reDrawSample(right, g, arrayToSort, picSamples);
                reDrawer.reDrawSample(left, g, arrayToSort, picSamples);
                refresher.refreshPicture(picSamples);
                Thread.Sleep(speed);
            }
            else
            {
                return right;
            }
        }
    }

    /// <summary>
    /// This recursive subroutine is responsible for sorting the array into the correct order after the individual partitions have been ordered.
    /// </summary>
    /// <param name="arr"></param>
    /// <param name="left"></param>
    /// <param name="right"></param>
    public void sortArray(ArrayList arr, int left, int right)
    {
        if (left < right)
        {
            int pivot = partition(arr, left, right);

            if (pivot > 1)
            {
                sortArray(arr, left, pivot - 1);
            }

            if (pivot + 1 < right)
            {
                sortArray(arr, pivot + 1, right);
            }
        }
    }

}

调用sortArray时,left = 0,right = 49&array是一个随机的50元素一维数组。

您可以忽略对reDrawer和refresher的引用,因为它们不影响排序算法,它们仅将结果绘制在图片框中。

1 个答案:

答案 0 :(得分:1)

  

当右==枢轴或左==枢轴时发生。

您是对的,在这种情况下,您将停止向左/向右递减。您需要在每次迭代中至少增加/减少一次。

public int partition(ArrayList arrayToSort, int left, int right)
{
    int pivot = (int)arrayToSort[left];
    left--;
    right++;  //To prevent the first iteration from ignoring the outermost elements
    ReDrawer reDrawer = new ReDrawer();
    Refresher refresher = new Refresher();

    while (true)
    {
        do
        {
            left++;
        }while (((IComparable)arrayToSort[left]).CompareTo(pivot) < 0);

        do
        {
            right--;
        }while (((IComparable)arrayToSort[right]).CompareTo(pivot) > 0);

        if (left < right)
        {
            object temp =arrayToSort[right];
            arrayToSort[right] = arrayToSort[left];
            arrayToSort[left] = temp;
            reDrawer.reDrawSample(right, g, arrayToSort, picSamples);
            reDrawer.reDrawSample(left, g, arrayToSort, picSamples);
            refresher.refreshPicture(picSamples);
            Thread.Sleep(speed);
        }
        else
        {
            return right;
        }
    }
}