Sorting
Swaps of neighbours to the hybrids libraries run: bubble, selection, and insertion sorts, gaps that shrink, halves merged, a pivot, a heap, the runs Timsort stitches, digits sorted in passes, and a network of comparators that sorts with no branches.
Algorithms & Data Structures
Before you start
Some lessons here build on lessons from other arcs: Heaps & Priority Queues.
- 1
Bubble Sortin progress
Algorithms · Sorting · ~15 minWatch the real library implementation sweep the array again and again, swapping neighbors until a pass finds nothing left to swap.
- 2
Selection Sortin progress
Algorithms · Sorting · ~15 minWatch the real library implementation scan for the smallest remaining bar and swap it to the front, one final position per pass.
- 3
Insertion Sortin review
Algorithms · Sorting · ~20 minWatch the real library implementation execute line by line while it grows a sorted prefix through the array.
- 4
Shellsortin progress
Algorithms · Sorting · ~20 minWatch the real library implementation insertion-sort the array at shrinking gaps, so far-apart bars move long distances in one hop.
- 5
Merge Sortin progress
Algorithms · Sorting · ~25 minWatch the real library implementation split the array down to single bars, then merge sorted halves back together through one auxiliary buffer.
- 6
Quicksortin progress
Algorithms · Sorting · ~25 minWatch the real library implementation partition the array around pivots that land in their final slots and never move again.
- 7
Heapsortin progress
Algorithms · Sorting · ~25 minWatch the real library implementation turn the array into a max heap, then retire the maximum to the end again and again.
- 8
Timsortin progress
Algorithms · Sorting · ~30 minWatch the real library implementation find natural runs, extend short ones with binary insertion, and merge them under the stack rules that keep it fast.
- 9
Radix Sortin review
Algorithms · Sorting · ~25 minWatch the real library implementation sort three-digit values without a single comparison: bucket by ones, then tens, then hundreds.
- 10
Sorting Networksin progress
Algorithms · Sorting · ~30 minWatch Batcher's odd-even merge network sort the array with a fixed schedule of compare-exchanges that never looks at the data to decide what happens next.