void qksort(int ilo, int ihi) {
int pivot; // pivot value for partitioning array
int ulo, uhi; // indices at ends of unpartitioned region
int ieq; // least index of array entry with value equal to pivot
int tempEntry; // temporary entry used for swapping
if (ilo >= ihi) {
return;
}
// Select a pivot value.
pivot = A[(ilo + ihi)/2];
// Initialize ends of unpartitioned region and least index of entry
// with value equal to pivot.
ieq = ulo = ilo;
uhi = ihi;
// While the unpartitioned region is not empty, try to reduce its size.
while (ulo <= uhi) {
if (A[uhi] > pivot) {
// Here, we can reduce the size of the unpartitioned region and
// try again.
uhi--;
} else {
// Here, A[uhi] <= pivot, so swap entries at indices ulo and
// uhi.
tempEntry = A[ulo];
A[ulo] = A[uhi];
A[uhi] = tempEntry;
// After the swap, A[ulo] <= pivot.
if (A[ulo] < pivot) {
// Swap entries at indices ieq and ulo.
tempEntry = A[ieq];
A[ieq] = A[ulo];
A[ulo] = tempEntry;
// After the swap, A[ieq] < pivot, so we need to change
// ieq.
ieq++;
// We also need to change ulo, but we also need to do
// that when A[ulo] = pivot, so we do it after this if
// statement.
}
// Once again, we can reduce the size of the unpartitioned
// region and try again.
ulo++;
}
}
// Now, all entries from index ilo to ieq - 1 are less than the pivot
// and all entries from index uhi to ihi + 1 are greater than the
// pivot. So we have two regions of the array that can be sorted
// recursively to put all of the entries in order.
qksort(ilo, ieq - 1);
qksort(uhi + 1, ihi);
}
void qksort(void) {
qksort(0, numEntries - 1);
}
void printArray(void) {
for (int i = 0; i < numEntries; i++) {
printf(" %d\n", A[i]);
}
printf("\n");
}
int main(void) {
printf("Before sorting, the entries of A are:\n");
printArray();
qksort();
printf("After sorting, the entries of A are:\n");
printArray();
}