Why Sorting Matters

The problems sorting solves and the techniques it enables.

Sorting is a preprocessing step that makes hard problems easy.

What sorting enables:

1.1. Binary search: Find elements in O(logn)O(\log n) instead of O(n)O(n).

2.2. Two pointers: Find pairs with a target sum in O(n)O(n) instead of O(n2)O(n^2).

3.3. Greedy algorithms: Many greedy solutions start by sorting (activity selection, interval scheduling).

4.4. Duplicate detection: Adjacent elements after sorting reveal duplicates in O(n)O(n).

5.5. Efficient merging: Merge kk sorted lists in O(length of arrays)O(\sum \text{length of arrays}) or O(n)O(n).

If you skip sorting, you'll write O(n2)O(n^2) solutions where O(nlogn)O(n \log n) exists.