Quick Sort Recursive
Quick sort is an efficient sorting algorithm, serving as a systematic method for placing the elements of an array in order.
static int Partition(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;
}
static void QuickSortRecursive(int* data, int left, int right) {
if (left < right)
{
int q = Partition(data, left, right);
QuickSortRecursive(data, left, q - 1);
QuickSortRecursive(data, q + 1, right);
}
}
Example
int data[] = { -1, 25, -58964, 8547, -119, 0, 78596 };
QuickSortRecursive(data, 0, 6);
Output
-58964
-119
-1
0
25
8547
78596