A beautiful, interactive guide to understanding one of the most important sorting algorithms in computer science.
Let's break it down in the simplest way possible.
Imagine you have a stack of 100 exams and you need to arrange them by score (lowest to highest).
Here's what Quick Sort does:
"Pick a reference, divide into smaller groups, sort each group the same way."
One of the fastest sorting algorithms for real-world data. Used everywhere from databases to video games!
Can sort "in-place" - meaning it doesn't need extra memory proportional to the input size.
The algorithm is beautifully simple and can be implemented in just a few lines of code.
Understanding the two key ideas behind Quick Sort
Quick Sort uses a technique called "Divide and Conquer" - a powerful problem-solving approach.
Split the problem into smaller, independent sub-problems
Solve each sub-problem recursively (the same way!)
Combine the solutions (in Quick Sort, this is automatic!)
The heart of Quick Sort is the partition operation. Let's understand it step by step:
Select an element to serve as the "reference point."
Use two pointers to scan from both ends.
Left Pointer (i)
Moves right, finds elements > pivot
Right Pointer (j)
Moves left, finds elements < pivot
When both stop: Swap the elements. Continue until pointers cross.
Watch Quick Sort in action, step by step
Click "Start Sorting" to begin
Pseudocode and implementation details
// Main QuickSort function
function quickSort(arr, low, high):
if low < high:
// Partition the array
pivotIndex = partition(arr, low, high)
// Recursively sort left part
quickSort(arr, low, pivotIndex - 1)
// Recursively sort right part
quickSort(arr, pivotIndex + 1, high)
// Partition function
function partition(arr, low, high):
// Choose last element as pivot
pivot = arr[high]
// i tracks the boundary of "smaller" elements
i = low - 1
// j scans through the array
for j = low to high - 1:
if arr[j] <= pivot:
i = i + 1
swap(arr[i], arr[j])
// Place pivot in correct position
swap(arr[i + 1], arr[high])
return i + 1
Quick Sort is an in-place algorithm, meaning it doesn't require extra space proportional to the input size. The partitioning rearranges elements within the original array!
Always pick arr[low]
â ď¸ Bad for sorted arrays
Always pick arr[high]
â Most common in teaching
Pick a random element
â Good average case
Median of first, middle, last
â Best in practice
Understanding how efficient Quick Sort really is
When the pivot always divides the array into roughly equal halves.
When does this happen?
When pivot is always (or nearly) the median element.
For randomly ordered arrays. This is what we expect in practice!
Why?
log n levels of recursion, each level does O(n) work.
When pivot is always smallest or largest element.
When does this happen?
Already sorted/reverse sorted array with first/last pivot.
The extra space comes from the call stack in recursive implementation.
| Algorithm | Best | Average | Worst | Space | Stable |
|---|---|---|---|---|---|
| Quick Sort | O(n log n) | O(n log n) | O(n²) | O(log n) | No |
| Merge Sort | O(n log n) | O(n log n) | O(n log n) | O(n) | Yes |
| Heap Sort | O(n log n) | O(n log n) | O(n log n) | O(1) | No |
| Bubble Sort | O(n) | O(n²) | O(n²) | O(1) | Yes |
Practice what you've learned
Your score: