⚡ Build the skills to land top-paying quant jobs. Offer ends in 17d 22h 18m 45s17 days 22 hours 18 minutes 45 seconds.
⚡ Build the skills to land top-paying quant jobs. Offer ends in 17d 22h 18m 45s17 days 22 hours 18 minutes 45 seconds.

Sign in to watch this lesson
Algorithms - Sorting and Searching · 5:34
We sort [5, 2, 4, 6, 1, 3] with insertion sort and watch the swap counter stop at 9, exactly the number of out-of-order pairs, which makes insertion sort O(n + inversions) and explains why it is fast on nearly sorted data (raced on this Mac) and why libc++'s std::sort uses it for tiny ranges. Selection and bubble sort are defined by their own invariants, and stability is shown on records with equal keys. Then a decision tree proves that any comparison sort needs about log2(n!) = Theta(n log n) comparisons.