C++ Program for Quick Sort Algorithm
partition function to divide the array and recursively sorts subarrays with quickSort(arr, low, high) calls.Examples
How to Think About It
Algorithm
Code
#include <iostream> using namespace std; int partition(int arr[], int low, int high) { int pivot = arr[high]; int i = low - 1; for (int j = low; j < high; j++) { if (arr[j] < pivot) { i++; swap(arr[i], arr[j]); } } swap(arr[i + 1], arr[high]); return i + 1; } void quickSort(int arr[], int low, int high) { if (low < high) { int pi = partition(arr, low, high); quickSort(arr, low, pi - 1); quickSort(arr, pi + 1, high); } } int main() { int arr[] = {5, 3, 8, 4, 2}; int n = sizeof(arr) / sizeof(arr[0]); quickSort(arr, 0, n - 1); cout << "Sorted array: "; for (int i = 0; i < n; i++) cout << arr[i] << " "; cout << endl; return 0; }
Dry Run
Let's trace the array {5, 3, 8, 4, 2} through the quick sort code.
Initial call
quickSort(arr, 0, 4) with arr = {5, 3, 8, 4, 2}
Partitioning
Pivot = 2; after partition arr = {2, 3, 8, 4, 5}, pivot index = 0
Left subarray
quickSort(arr, 0, -1) stops (no elements)
Right subarray
quickSort(arr, 1, 4) with arr = {3, 8, 4, 5}
Partitioning right subarray
Pivot = 5; after partition arr = {3, 4, 5, 8}, pivot index = 3
Sort left of pivot
quickSort(arr, 1, 2) with arr = {3, 4}
Partitioning left of pivot
Pivot = 4; after partition arr = {3, 4}, pivot index = 2
Recursion ends
Subarrays of size 1 or 0 stop recursion
| Step | Array State | Pivot | Pivot Index |
|---|---|---|---|
| 1 | {5, 3, 8, 4, 2} | 2 | 0 |
| 2 | {2, 3, 8, 4, 5} | 5 | 3 |
| 3 | {2, 3, 4, 5, 8} | 4 | 2 |
Why This Works
Step 1: Choosing pivot
The pivot divides the array into smaller and larger parts using partition.
Step 2: Partitioning
Elements less than pivot move left, greater move right, placing pivot in correct position.
Step 3: Recursion
Quick sort repeats on subarrays left and right of pivot until fully sorted.
Alternative Approaches
#include <iostream> using namespace std; int partition(int arr[], int low, int high) { int pivot = arr[high]; int i = low - 1; for (int j = low; j < high; j++) { if (arr[j] <= pivot) { i++; swap(arr[i], arr[j]); } } swap(arr[i + 1], arr[high]); return i + 1; } void quickSort(int arr[], int low, int high) { if (low < high) { int pi = partition(arr, low, high); quickSort(arr, low, pi - 1); quickSort(arr, pi + 1, high); } } int main() { int arr[] = {5, 3, 8, 4, 2}; int n = sizeof(arr) / sizeof(arr[0]); quickSort(arr, 0, n - 1); cout << "Sorted array: "; for (int i = 0; i < n; i++) cout << arr[i] << " "; cout << endl; return 0; }
#include <iostream> using namespace std; int partition(int arr[], int low, int high) { int pivot = arr[low]; int i = low - 1, j = high + 1; while (true) { do { i++; } while (arr[i] < pivot); do { j--; } while (arr[j] > pivot); if (i >= j) return j; swap(arr[i], arr[j]); } } void quickSort(int arr[], int low, int high) { if (low < high) { int pi = partition(arr, low, high); quickSort(arr, low, pi); quickSort(arr, pi + 1, high); } } int main() { int arr[] = {5, 3, 8, 4, 2}; int n = sizeof(arr) / sizeof(arr[0]); quickSort(arr, 0, n - 1); cout << "Sorted array: "; for (int i = 0; i < n; i++) cout << arr[i] << " "; cout << endl; return 0; }
Complexity: O(n log n) average, O(n^2) worst time, O(log n) space
Time Complexity
Quick sort divides the array and sorts subarrays recursively. Average case is O(n log n) because each partition splits the array roughly in half. Worst case is O(n^2) when pivot choices are poor, like always smallest or largest element.
Space Complexity
Quick sort uses O(log n) space due to recursion stack. It sorts in place, so no extra arrays are needed.
Which Approach is Fastest?
Hoare partition is often faster due to fewer swaps, but Lomuto is simpler and more common for teaching.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Lomuto partition | O(n log n) avg, O(n^2) worst | O(log n) | Simplicity and teaching |
| Hoare partition | O(n log n) avg, O(n^2) worst | O(log n) | Performance with fewer swaps |