Codele / Algorithms
Selection and insertion sort
Two more simple ways to put things in order. One keeps choosing the smallest; the other deals cards into your hand one at a time.
- Selection sort: find the smallest bar in the unsorted part and swap it to the front. Repeat for the next place.
- Insertion sort: take the next bar and slide it left until the bar before it is smaller, like sorting a hand of playing cards.
- Both keep a sorted part that grows by one bar each round.
- Selection always scans everything left, even if it is sorted. Insertion does almost nothing on a nearly sorted row.
- Both are O(n²): fine for short rows, slow for long ones.
Loading the playable lesson…