Why Sort?
Sorting arranges data into order, making searching, grouping, and deduplication trivial.
| Category | Complexity | Key Algorithms |
|---|---|---|
| Simple | Bubble, Selection, Insertion | |
| Efficient | Merge, Quicksort, Heapsort |
- Bubble sort: swaps adjacent out-of-order elements.
- Selection sort: finds the smallest remaining element.
- Insertion sort: builds a sorted prefix, inserting new elements. Fast on nearly-sorted or small inputs.
Efficient Sorts & Stability
- Merge sort: divides list in half, recurses, and merges sorted halves. Always and stable.
- Quicksort: partitions around a pivot ( average, worst case).
- Heapsort: extracts from a heap ( guaranteed).
Stability preserves the relative order of equal elements. Merge sort is stable; Quicksort usually is not.
Pitfall: is the comparison lower bound (). However, Insertion sort can beat efficient sorts on small inputs due to low overhead.