Quick Sort Recursive
Quick sort is an efficient sorting algorithm, serving as a systematic method for placing the elements of an array in order.
public static void QuickSortRecursive(ref int[] data, int left, int right)
{
if (left < right)
{
int q = Partition(ref data, left, right);
QuickSortRecursive(ref data, left, q - 1);
QuickSortRecursive(ref data, q + 1, right);
}
}
private static int Partition(ref int[] data, int left, int right)
{
int pivot = data[right];
int temp;
int i = left;
for (int j = left; j < right; ++j)
{
if (data[j] <= pivot)
{
temp = data[j];
data[j] = data[i];
data[i] = temp;
i++;
}
}
data[right] = data[i];
data[i] = pivot;
return i;
}
Example
int[] data = new int[] { -1, 25, -58964, 8547, -119, 0, 78596 };
QuickSortRecursive(ref data, 0, data.Length - 1);
Output
-58964
-119
-1
0
25
8547
78596