Price your future right. 60% off with code BLACKSCHOLES at USD checkout. Offer ends in 1d 16h 01m 37s1 days 16 hours 1 minutes 37 seconds.
Price your future right. 60% off with code BLACKSCHOLES at USD checkout. Offer ends in 1d 16h 01m 37s1 days 16 hours 1 minutes 37 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.