publicMan
publicMan

Reputation: 19

Quicksort in descending order c# (No built in sort functions)

So I have implemented a quicksort algorithm :

 public static void Quick_Sort(int[] data, int left, int right,int dataSet)
{
    int i = left, j = right;
    int pivot, temp;
    dataSet tempSet;
    pivot = data[(left + right) / 2];
    do
    {
        while ((data[i] < pivot) && (i < right)) i++;
        while ((pivot < data[j]) && (j > left)) j--;
        if (i <= j)
        {
            //First change the data Array
            temp = data[i];
            data[i] = data[j];
            data[j] = temp;
            //Then change the dataSet array
            if (dataSet == 1)
            {
                tempSet = Program.set1Data[i];
                Program.set1Data[i] = Program.set1Data[j];
                Program.set1Data[j] = tempSet;
            }
            else if (dataSet == 2)
            {
                tempSet = Program.set2Data[i];
                Program.set2Data[i] = Program.set2Data[j];
                Program.set2Data[j] = tempSet;
            }
            else if (dataSet ==3)
            {
                tempSet = Program.bothSetData[i];
                Program.bothSetData[i] = Program.bothSetData[j];
                Program.bothSetData[j] = tempSet;
            }

            i++;
            j--;
        }
    } while (i <= j);
    if (left < j) Quick_Sort(data, left, j,dataSet);
    if (i < right) Quick_Sort(data, i, right,dataSet);
}

And I'm wondering how to quicksort it in descending order. Now, I actually need to sort the array, I can't just use something like Array.Reverse() to get my desired result. Many Thanks.


EDIT I've change it to : while ((data[i] > pivot) && (i > right)) i++; while ((pivot > data[j]) && (j < left)) j--;

Following Mhd's suggestion and it now works as desired.

Upvotes: 1

Views: 1907

Answers (2)

Jakob
Jakob

Reputation: 430

If you want a little more generic solution that can be applied to anything that implements the Ilist interface this could be a way to do it, then you can just pass in a false flag for descending order:

public static void QuickSort(IList list, int start, int end, bool asc = true)
{
    if (start >= end)
    {
        return;
    }

    int i = Partition(list, start, end, asc);

    QuickSort(list, start, i - 1, asc);
    QuickSort(list, i + 1, end, asc);
}

private static int Partition(IList list, int start, int end, bool asc)
{
    object temp;
    object p = list[end];
    int i = start - 1;

    for (int j = start; j <= end - 1; j++)
    {
        if (asc)
        {
            if (((IComparable)list[j]).CompareTo(p) <= 0)
            {
                i++;
                temp = list[i];
                list[i] = list[j];
                list[j] = temp;
            }
        }
        else
        {
            if (((IComparable)list[j]).CompareTo(p) >= 0)
            {
                i++;
                temp = list[j];
                list[j] = list[i];
                list[i] = temp;
            }
        }

    }


    temp = list[i + 1];
    list[i + 1] = list[end];
    list[end] = temp;
    return i + 1;
}

Upvotes: 0

Mhd
Mhd

Reputation: 2978

Just change:

while ((data[i] < pivot) && (i < right)) i++;
while ((pivot < data[j]) && (j > left)) j--;

to:

while ((data[i] > pivot) && (i < right)) i++;
while ((pivot > data[j]) && (j > left)) j--;

Upvotes: 3

Related Questions