Quick Sort
A highly efficient Divide and Conquer algorithm. It picks an element as pivot and partitions the given array around the picked pivot.
Quick Sort Algorithm
Quick Sort: Level 0 (partition)
Starting Quick Sort with 7 elements
Current array: [29, 10, 14, 37, 13, 8, 25] (length: 7)
Partition around a pivot
Order one level of quick sort (Lomuto partition).
Tap a piece, then tap where it goes — or drag it. Tap the tray to take one back.
- 1Drop a step here
- 2Drop a step here
- 3Drop a step here
- 4Drop a step here
- 5Drop a step here
Test your knowledge
What is the "Partition" step in Quick Sort?
The short version
Imagine a teacher lining students up by height. She picks one student (the pivot) and says: 'Shorter to the left, taller to the right.' Now she has two smaller groups and repeats. Much faster than moving one student at a time!
In more depth
Partitioning is Key
Quick Sort revolves around the idea of Partitioning. By picking a "pivot" element and organizing the array so that everything smaller is on the left and everything larger is on the right, we effectively place the pivot in its final, correct position.
Why is it so fast?
While its worst-case time complexity is O(n²), its average case is O(n log n) with very small constant factors. It sorts in-place (using little extra memory) and is cache-friendly, often outperforming Merge Sort and Heap Sort in practice on arrays.
Watch out for:
The choice of pivot matters! Picking the first or last element in a sorted array leads to the dreaded O(n²) worst case. Randomized pivots or "Median of Three" helps avoid this.
When to reach for this
- General purpose sorting (standard library implementations often use it)
- When **Space Complexity** matters (O(log n) stack space vs O(n) for Merge Sort)
- When you don't need a Stable Sort
Interview Questions
References & Further Reading
Love this visualization?
Sign up to track your progress, earn achievements, use the AI assistant, and access your personal learning dashboard.
100% free • No credit card required