且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

快速排序不能正确排序

更新时间:2023-11-10 12:44:34

在除了我的评论这个问题本身,我想指出的是,切换() DoQuickSort()方法并不需要返回数组(按我在这也解释了该数组是一个参考本身的注释注)。为此,你的代码做的工作应该是这样的:

 公众诠释分区(INT []数组,INT左,右INT,INT pivotIndex)
{
INT pivValue =阵列[pivotIndex]

互换(数组,pivotIndex,右);

INT storeIndex =左;

的for(int i =左; I<权利;我++)
{
如果(阵列[1] - = pivValue)
{
交换(数组,我,storeIndex);
storeIndex = storeIndex + 1;
}
}

互换(数组,storeIndex,右);

返回storeIndex;
}

公共无效掉期(INT []编曲,诠释第一,诠释秒)
{
INT TEMP = ARR [首页];
ARR [首页] = ARR [秒];
ARR [秒] =温度;
}

公共无效DoQuickSort(INT []数组,诠释左,诠释右)
{
如果(右GT;左)
{
INT pivIndex =(左+右)/ 2;
INT newPivIndex =分区(数组,左,右,pivIndex);
DoQuickSort(阵,左,newPivIndex - 1);
DoQuickSort(数组,newPivIndex + 1,右);
}
}


Attempting to learn from doing an implementation of Quicksort, I cannot find out why it's not sorting properly.

Using this sequence:

6, 7, 12, 5, 9, 8, 65, 3

It returns this:

3, 5, 7, 8, 9, 65, 12, 6

It seems to sort somewhat, but not all. What have I missed?

Here's my code:

 static void Main(string[] args)
        {
            QuickSort qs = new QuickSort();

           int[] arr = new int[] { 6, 7, 12, 5, 9, 8, 65, 3 };

            foreach (int l in arr)
            {
                Console.Write(l + ", ");
            }

            int left = 0;
            int right = arr.Count() - 1;

            int[] arrr = qs.DoQuickSort(ref arr, left, right);
            Console.WriteLine("Sorted List: ");
            foreach (int i in arrr)
            {
                Console.Write(i + " ");
            }



            Console.ReadLine();
        }

   public int Partition(int[] array, int left, int right, int PivotIndex)
    {
    int pivValue = array[PivotIndex];

    array = Swap(ref array, PivotIndex, right);

    int storeIndex = left;

    for (int i = PivotIndex; i < right-1; i++)
    {
        if (array[i] <= pivValue)
        {
            array = Swap(ref array, i, storeIndex);
            storeIndex = storeIndex + 1;
        }
    }

    array = Swap(ref array, storeIndex, right);

    return storeIndex;
}

public int[] Swap(ref int[] arr, int first, int second)
{
    int temp = arr[first];
    arr[first] = arr[second];
    arr[second] = temp;

    return arr;
}

public int[] DoQuickSort(ref int[] array, int left, int right)
{
    if (right > left)
    {
        int PivIndex = (left + right) / 2;
        int newPivIndex = Partition(array, left, right, PivIndex);
        DoQuickSort(ref array, left, newPivIndex - 1);
        DoQuickSort(ref array, newPivIndex + 1, right);
    }

    return array;
}

In addition to my comment on the question itself, I wanted to point out that the Swap() and DoQuickSort() methods do not need to return the array (as per my note in the comment which explains that the array is a reference itself). To that end, your code to do the job should look like the following:

public int Partition(int[] array, int left, int right, int pivotIndex)
{
    int pivValue = array[pivotIndex];

    Swap(array, pivotIndex, right);

    int storeIndex = left;

    for (int i = left; i < right; i++)
    {
        if (array[i] <= pivValue)
        {
            Swap(array, i, storeIndex);
            storeIndex = storeIndex + 1;
        }
    }

    Swap(array, storeIndex, right);

    return storeIndex;
}

public void Swap(int[] arr, int first, int second)
{
    int temp = arr[first];
    arr[first] = arr[second];
    arr[second] = temp;
}

public void DoQuickSort(int[] array, int left, int right)
{
    if (right > left)
    {
        int pivIndex = (left + right) / 2;
        int newPivIndex = Partition(array, left, right, pivIndex);
        DoQuickSort(array, left, newPivIndex - 1);
        DoQuickSort(array, newPivIndex + 1, right);
    }
}