1

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.

2

2 Answers 2

3

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--;
Sign up to request clarification or add additional context in comments.

1 Comment

Just tried this but it doesn't seemed to have work. Still all jumbled. Would i need to inverse the other comparison in the other condition too?
0

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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.