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