β‘ What Is Quick Sort?
Quick Sort is a divide-and-conquer sorting algorithm.
That means instead of sorting the whole list at once, it divides the list into smaller parts, sorts each part, and then combines them back together.
Think of it like this β if you want to organize a messy pile of books, you might:
- Pick one book as a reference (say, based on its height).
- Move all shorter books to one side and all taller books to the other.
- Then repeat the same process for each smaller pile.
By the end, every book ends up in the right order β without ever comparing every single one directly with every other.
π§© The Core Idea
Quick Sort works around one key element called the pivot.
Hereβs what happens:
- Choose a pivot (any one element from the list β often the first, last, or middle).
- Rearrange the list so that:
- All elements smaller than the pivot come before it.
- All elements greater than the pivot come after it.
This step is called partitioning.
- Recursively apply the same process to the left and right sublists.
- When every sublist has one element left, the whole list is sorted!
π» Letβs Understand With an Example
Say we have this unsorted list:
[9, 3, 7, 1, 6, 2]
Weβll sort it in ascending order.
Step 1: Pick a pivot
Letβs pick the last element as the pivot: 2.
Now we compare each element with the pivot and rearrange:
- 9 > 2 β goes to the right.
- 3 > 2 β right.
- 7 > 2 β right.
- 1 < 2 β left.
- 6 > 2 β right.
After rearranging:
[1, 2, 9, 3, 7, 6]
Now the pivot 2 is in its correct final position.
Everything on the left is smaller, and everything on the right is greater.
Step 2: Repeat on sublists
We now do the same for the two sides.
Left sublist: [1] β already sorted (only one element).
Right sublist: [9, 3, 7, 6]
Pick pivot = 6.
Compare and rearrange:
- 9 > 6 β right
- 3 < 6 β left
- 7 > 6 β right
Result after partitioning:
[3, 6, 9, 7]
Now 6 is in its correct spot.
Left = [3], Right = [9, 7]
Step 3: Sort the right part [9, 7]
Pivot = 7.
- 9 > 7 β right
After rearranging: [7, 9]
Everything is now sorted! π
β Final Sorted List:
[1, 2, 3, 6, 7, 9]
πΌοΈ Diagram: Quick Sort Visualization
Original: [9, 3, 7, 1, 6, 2]
Pivot = 2
/ \
[1] [9,3,7,6]
Pivot = 6
/ \
[3] [9,7]
Pivot = 7
/ \
[] [9]
Final Sorted: [1, 2, 3, 6, 7, 9]
Each time we choose a pivot, the list splits into smaller and smaller groups until every element falls into its perfect place.
π§ Why Is It Called βQuickβ?
Because itβs fast on average.
Unlike Bubble Sort (which compares every element with every other), Quick Sort avoids unnecessary work by cutting the list into smaller chunks each time.
It doesnβt have to sort whatβs already sorted β thatβs the beauty of divide and conquer.
βοΈ Pseudocode (in simple terms)
QuickSort(arr, low, high):
if low < high:
pivotIndex = partition(arr, low, high)
QuickSort(arr, low, pivotIndex - 1)
QuickSort(arr, pivotIndex + 1, high)
partition(arr, low, high):
pivot = arr[high]
i = low - 1
for j from low to high - 1:
if arr[j] <= pivot:
i = i + 1
swap(arr[i], arr[j])
swap(arr[i + 1], arr[high])
return (i + 1)
β±οΈ Time and Space Complexity
| Case | Time Complexity | Explanation |
|---|---|---|
| Best Case | O(n log n) | Pivot divides list evenly. |
| Average Case | O(n log n) | Typical performance. |
| Worst Case | O(nΒ²) | Happens if pivot is always smallest or largest (unbalanced splits). |
Space Complexity: O(log n) (due to recursive calls).
πΌ Advantages
β
Very fast for large lists.
β
Works well in practice and used widely in real-world systems.
β
In-place sorting β doesnβt need extra memory.
β οΈ Disadvantages
β Performance drops if the pivot selection is poor (e.g., already sorted list).
β Recursive calls can use a lot of stack space.
π Real-Life Analogy
Imagine sorting a deck of cards by number.
You pick one card (say, 7) as a reference.
You move all smaller cards to one pile and larger ones to another.
Then you pick new pivots in each pile and repeat.
Eventually, you get a perfectly sorted deck without checking every card against every other card. π
Thatβs Quick Sort β smart, organized, and efficient.
π§ In a Nutshell
| Feature | Description |
|---|---|
| Type | Divide and Conquer |
| Key Concept | Pivot-based partitioning |
| Average Speed | O(n log n) |
| Best For | Large datasets |
| Not Ideal For | Already sorted or very small data |
π¨ Simple Summary Diagram
[9, 3, 7, 1, 6, 2]
β
Pick pivot = 2
β [1] + [2] + [9,3,7,6]
β
Pick pivot = 6
β [3] + [6] + [9,7]
β
Pick pivot = 7
β [] + [7] + [9]
Final β [1, 2, 3, 6, 7, 9]