mediumSortingDivide and ConquerArrays & Hashing

Quick Sort

mediumTime: O(n log n)Space: O(log n)

Signals to notice

partition around pivot, sort partitionsaverage O(n log n)in-place sorting

Brute force first

Not applicable — quicksort IS a sorting algorithm. It is a fair place to begin because it matches the surface of the question, yet it does not capture the deeper structure that makes the problem simpler.

The key insight

Choose pivot, partition: elements < pivot go left, > pivot go right. Recursively sort both sides. Average, worst with bad pivots. space for recursion stack. Once you hold onto the right piece of information from moment to moment, the problem feels less like trial and error and more like following a shape that was there all along.

What must stay true

After partitioning, the pivot is in its final sorted position. All elements left of it are smaller; all right are larger. Recursion handles the sub-arrays. When you keep that truth intact, each local choice supports the larger solution instead of fighting it.

Easy way to go wrong

Always choosing the first or last element as pivot — this gives on already-sorted input. Use random pivot or median-of-three for better average performance. When the code becomes mechanical before the idea is clear, small edge cases start breaking the whole story.

Arrays & Hashing Pattern