0
Challenge · 30Sort & Search

Sorting as a setup move

Patternsused in ~95% of apps

You almost never write a sort. You decide whether to call one, and that decision, made in about ten seconds, often turns a problem you cannot see into one you can.

Core idea. Sorting is not the answer; it is what makes the answer visible. Once the data is in order, equal things become neighbours, extremes sit at the ends, and a search can halve the list. Pay O(n log n) once, then use the structure you just bought.
Playground, this isn't a screenshot
what sorting buys you
are there duplicates?compare every pair, O(n²)duplicates are neighbours, one walk
is a value present?check every item, O(n)halve the list each step, O(log n)
the k largest?scan k timesread off the end
do intervals overlap?compare every paironly neighbours can touch
the original positions?you have themdestroyed by the sort
O(1) space required?finesort() allocates. Check the rule

The last two rows are the price. Sorting is a trade, not a free upgrade, and knowing when not to sort is half the skill.

↑ play a little first, then dig in