Understanding Quick Sort in Detail
Quick Sort is one of the most efficient and widely used algorithms in Computer Science. Unlike Bubble Sort, which iterates and swaps, Quick Sort uses a Divide and Conquer strategy. It works by selecting a "pivot" element and partitioning the array around it, such that smaller elements move to the left and larger elements move to the right.
Imagine a librarian trying to organize a messy pile of books. Instead of comparing every book to every other book, the librarian picks one random book (the pivot)—say, "Harry Potter". She then quickly throws every book alphabetically before Harry Potter into a left pile, and every book after it into a right pile. Now, "Harry Potter" is in its exact final spot. She then repeats this process for the left pile and the right pile until every book is shelved.
The Logic Behind the Partition
The core mechanism of Quick Sort is Partitioning. This process dictates how the array is split before the recursive calls are made.
- Pick a Pivot: The algorithm selects an element to serve as the reference point (commonly the last element, first element, or median).
- Rearrange: It scans the array. Any element smaller than the pivot is moved to the left side; any element larger is moved to the right side.
- Place Pivot: The pivot is placed in the "middle" between the two sections.
- Recursion: The algorithm recursively applies the same logic to the left sub-array and the right sub-array.
Step-by-Step Algorithm Trace
Let's trace the partitioning logic using the last element as the pivot.
Input Array: [10, 80, 30, 90, 40, 50, 70]
Step 1: Partitioning around 70 (Pivot)
- We scan from left to right.
- 10 < 70 (Keep left)
- 80 > 70 (Stop, needs to move right)
- 30 < 70 (Swap 30 and 80). Array:
[10, 30, 80, 90, 40, 50, 70] - 90 > 70 (Stop)
- 40 < 70 (Swap 40 and 90). Array:
[10, 30, 40, 90, 80, 50, 70] - 50 < 70 (Swap 50 and 80). Array:
[10, 30, 40, 50, 90, 80, 70] - Finally, place Pivot (70) in the correct position (swap with first larger element, 90).
Result: [10, 30, 40, 50, 70, 80, 90]
Note: 70 is now sorted. We now recursively sort the left side [10...50] and right side [80, 90].
Code Implementation
Below is the implementation of Quick Sort in multiple programming languages. The partition function is the heart of the algorithm, efficiently rearranging elements around the pivot.
#include <stdio.h>void swap(int* a, int* b) {int temp = *a;*a = *b;*b = temp;}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[] = {10, 7, 8, 9, 1, 5};int n = sizeof(arr) / sizeof(arr[0]);quickSort(arr, 0, n - 1);printf("Sorted array: ");for (int i = 0; i < n; i++)printf("%d ", arr[i]);return 0;}
Time & Space Complexity Analysis
Quick Sort is generally preferred over Bubble Sort because of its superior average-case performance.
Time Complexity
This occurs when the pivot divides the array into roughly equal halves. The tree depth is log n, and each level requires n work.
This happens when the pivot is always the smallest or largest element (e.g., sorting an already sorted array with the last element as pivot). This creates unbalanced partitions.
Space Complexity
While Quick Sort is an in-place sort (it doesn't create a new array), it requires stack space for the recursive calls. In the worst case, this stack depth can grow to O(n), but on average it is O(log n).
When to Use Quick Sort?
Quick Sort is often the default sorting algorithm in many standard libraries (like C++ std::sort or Java's Arrays.sort for primitives) because of its speed and cache efficiency.
Large Datasets
It is excellent for general-purpose sorting of large arrays where average-case performance matters most.
Memory Constraints
Since it is an in-place sort, it doesn't require the extra memory allocation that Merge Sort does.
Virtual Memory
Quick Sort has good "locality of reference," meaning it works well with computer cache and virtual memory systems, making it faster in practice than other O(n log n) algorithms like Heap Sort.