Insertion sort
A sorting algorithm that takes elements one at a time and inserts each into its place within the already sorted part, like arranging cards in your hand.
Example
With [7, 3, 5]: 3 goes before 7 → [3, 7], and 5 goes between them → [3, 5, 7].
Test yourself: Insertion sort
What is an algorithm
For what size of data does insertion sort work well?
With 10 cards it's about 45 comparisons in the worst case; with 1 million items it'd be about 500 billion. That's why many languages use it only for small lists.
Insertion sort
In insertion sort, at the start, the ___ element already counts as sorted.
A 1-element list is always sorted. With 10 elements, the other 9 are inserted one at a time.
Insertion sort
With [2, 6, 9] already sorted, inserting 5 shifts ___ elements.
9 and 6 are larger than 5 and shift; 2 is smaller and stays. You get [2, 5, 6, 9].
0/3
â–¶ PlayLessons that use it
What is an algorithm
You already use algorithms
Measuring efficiency
Fast and light on memory
What makes a good sort
5 things you ask of it
Selection sort
Picking the smallest, over and over
Bubble sort
The largest rises like a bubble
Insertion sort
Like arranging the cards in your hand
Merge sort
Split in half and merge in order
