Skip to main content

Quick Sort

Quick Sort is an efficient sorting algorithm widely used in practical scenarios due to its low average time complexity. It employs the divide-and-conquer approach by selecting a "pivot" element to partition the array into two parts and recursively sorting the subarrays. This article explains the principle and steps of Quick Sort in detail, providing code examples in C and Go to help beginners get started quickly.

Principle

The core idea of Quick Sort is:

  1. Select a pivot element (usually the first, last, or a random element of the array).
  2. Partition the array into two parts: elements smaller than the pivot are placed on the left, and elements greater than the pivot are placed on the right (the partitioning process).
  3. Recursively apply Quick Sort to the left and right subarrays.
  4. When the subarray has a length of 1 or 0, the recursion stops.

Its average time complexity is O(n log n), where n is the length of the array, with a worst-case complexity of O(n²) (e.g., if the array is already sorted or reverse-sorted). Despite the worst-case scenario, Quick Sort is generally faster in practice than Insertion Sort or Bubble Sort.

Steps

Assuming we want to sort an integer array in ascending order, here are the detailed steps for Quick Sort:

  1. Choose a Pivot: Select an element from the array as the pivot, for example, the last element.
  2. Partition: Rearrange the array so that elements smaller than the pivot are on its left, and elements greater than the pivot are on its right, with the pivot element in the middle.
  3. Recursion: Recursively call Quick Sort on the subarrays to the left and right of the pivot.
  4. Termination: When a subarray has a length of less than or equal to 1, the sorting is complete.

Implementation

Here is the C implementation of Quick Sort, with detailed comments to aid understanding.

main.c
#include <stdio.h>

// Swap two elements
void swap(int* a, int* b) {
int temp = *a;
*a = *b;
*b = temp;
}

// Partition function: selects the last element as the pivot
int partition(int arr[], int low, int high) {
int pivot = arr[high]; // Select the last element as the pivot
int i = low - 1; // Boundary of the region with elements smaller than the pivot

for (int j = low; j < high; j++) {
if (arr[j] <= pivot) {
i++; // Expand the region of smaller elements
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]); // Place the pivot in its correct position
return i + 1; // Return the pivot's index
}

// Quick Sort function
void quickSort(int arr[], int low, int high) {
if (low < high) {
// Get the pivot index after partitioning
int pi = partition(arr, low, high);

// Recursively sort the left half
quickSort(arr, low, pi - 1);
// Recursively sort the right half
quickSort(arr, pi + 1, high);
}
}

// Helper function to print an array
void printArray(int arr[], int n) {
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}

// Main function to test quick sort
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);

printf("Original array: ");
printArray(arr, n);

quickSort(arr, 0, n - 1);

printf("Sorted array: ");
printArray(arr, n);

return 0;
}
Compile & Run
$ gcc -o bin/quick-sort main.c
$ bin/quick-sort
Original array: 64 34 25 12 22 11 90
Sorted array: 11 12 22 25 34 64 90
$

Summary

The core of Quick Sort is to achieve efficient sorting by selecting a pivot element to partition the array into two parts and recursively sorting them.


Copyright Notice: Freely reproduced, non-commercial, no derivatives, attribution, and keep the source.

Author: afxcn

Source: https://gostartkit.com/docs/algo/quick-sort

Date Published: June 28, 2025