2024-02-03 03:36:20 +00:00
---
title: Insertion Sort
TARGET DECK: Obsidian::STEM
2024-02-08 14:02:59 +00:00
FILE TAGS: algorithm::sorting
2024-02-03 03:36:20 +00:00
tags:
- algorithm
- sorting
---
## Overview
2024-02-09 18:50:56 +00:00
Property | Value
----------- | --------
Best Case | $\Omega(n)$
Worst Case | $O(n^2)$
Avg. Case | $O(n^2)$
Aux. Memory | $O(1)$
Stable | Yes
Adaptive | Yes
2024-02-03 03:36:20 +00:00
2024-02-08 14:02:59 +00:00
![[insertion-sort.gif]]
2024-02-03 03:36:20 +00:00
2024-02-11 12:33:02 +00:00
%%ANKI
Basic
2024-03-06 20:25:34 +00:00
Describe `INSERTION_SORT` in a single sentence.
2024-02-11 12:33:02 +00:00
Back: Repeatedly put the next record into a sorted array from right to left.
2024-03-19 00:28:34 +00:00
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
2024-02-11 12:33:02 +00:00
<!-- ID: 1707589393194 -->
END%%
2024-02-03 03:36:20 +00:00
%%ANKI
Basic
2024-03-06 20:25:34 +00:00
What is `INSERTION_SORT` 's best case runtime?
2024-02-09 18:50:56 +00:00
Back: $\Omega(n)$
2024-03-19 00:28:34 +00:00
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
2024-02-03 03:36:20 +00:00
<!-- ID: 1706925879541 -->
END%%
%%ANKI
Basic
2024-03-06 20:25:34 +00:00
What input value does `INSERTION_SORT` perform best on?
2024-02-03 03:36:20 +00:00
Back: An already sorted array.
2024-03-19 00:28:34 +00:00
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
2024-02-03 03:36:20 +00:00
<!-- ID: 1706925921544 -->
END%%
%%ANKI
Basic
2024-03-06 20:25:34 +00:00
What is `INSERTION_SORT` 's worst case runtime?
2024-02-03 03:36:20 +00:00
Back: $O(n^2)$
2024-03-19 00:28:34 +00:00
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
2024-02-03 03:36:20 +00:00
<!-- ID: 1706926586947 -->
END%%
%%ANKI
Basic
2024-03-06 20:25:34 +00:00
What input value does `INSERTION_SORT` perform worst on?
2024-02-03 03:36:20 +00:00
Back: An array in reverse-sorted order.
2024-03-19 00:28:34 +00:00
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
2024-02-03 03:36:20 +00:00
<!-- ID: 1706926586951 -->
END%%
2024-02-08 14:02:59 +00:00
%%ANKI
Basic
2024-03-06 20:25:34 +00:00
What is `INSERTION_SORT` 's average case runtime?
2024-02-08 14:02:59 +00:00
Back: $O(n^2)$
2024-03-19 00:28:34 +00:00
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
2024-02-08 14:02:59 +00:00
<!-- ID: 1707329732933 -->
END%%
2024-02-03 03:36:20 +00:00
%%ANKI
Basic
2024-03-06 20:25:34 +00:00
Is `INSERTION_SORT` in place?
2024-04-14 17:58:01 +00:00
Back: Yes.
2024-03-19 00:28:34 +00:00
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
2024-02-03 03:36:20 +00:00
<!-- ID: 1706926586955 -->
END%%
%%ANKI
Basic
2024-03-06 20:25:34 +00:00
Is `INSERTION_SORT` stable?
2024-04-14 17:58:01 +00:00
Back: Yes.
2024-03-19 00:28:34 +00:00
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
2024-02-03 03:36:20 +00:00
<!-- ID: 1706926586959 -->
END%%
2024-02-09 18:50:56 +00:00
%%ANKI
B asic
2024-03-06 20:25:34 +00:00
Is `INSERTION_SORT` adaptive?
2024-04-14 17:58:01 +00:00
Back: Yes.
2024-03-19 00:28:34 +00:00
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
2024-02-09 18:50:56 +00:00
<!-- ID: 1707504634779 -->
END%%
2024-02-03 03:36:20 +00:00
```c
void insertion_sort(const int n, int A[static n]) {
for (int i = 1; i < n ; + + i ) {
int key = A[i];
int j = i - 1;
for (; j >= 0 & & A[j] > key; --j) {
A[j + 1] = A[j];
}
A[j + 1] = key;
}
}
```
%%ANKI
Basic
2024-02-08 14:02:59 +00:00
What sorting algorithm does the following demonstrate?
![[insertion-sort.gif]]
2024-03-06 20:25:34 +00:00
Back: `INSERTION_SORT`
2024-03-19 00:28:34 +00:00
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
2024-02-08 14:02:59 +00:00
<!-- ID: 1707400559085 -->
END%%
## Loop Invariant
Consider [[loop-invariant|loop invariant]] $P$ given by
2024-04-29 17:12:56 +00:00
> `A[0:i-1]` consists of the original `A[0:i-1]` elements but in sorted order.
2024-02-08 14:02:59 +00:00
We prove $P$ maintains the requisite properties:
* Initialization
2024-04-29 17:12:56 +00:00
* When `i = 1` , `A[0:0]` contains a single element. This trivially satisfies $P$.
2024-02-08 14:02:59 +00:00
* Maintenance
2024-04-29 17:12:56 +00:00
* Suppose $P$ holds for some `1 ≤ i < n` . Then `A[0:i-1]` consists of the original `A[0:i-1]` elements but in sorted order. On iteration `i + 1` , the nested for loop puts `A[0:i]` in sorted order. At the end of the iteration, `i` is incremented meaning `A[0:i-1]` still satisfies $P$.
2024-02-08 14:02:59 +00:00
* Termination
2024-04-29 17:12:56 +00:00
* The loop ends because `i < n` is no longer true. Then `i = n` . Since $P$ holds, this means `A[0:n-1]` , the entire array, is in sorted order.
2024-02-08 14:02:59 +00:00
%%ANKI
Basic
2024-04-29 17:12:56 +00:00
Given array `A[0:n-1]` , what is `INSERTION_SORT` 's loop invariant?
Back: `A[0:i-1]` consists of the original `A[0:i-1]` elements but in sorted order.
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
2024-02-08 14:02:59 +00:00
<!-- ID: 1707332638371 -->
END%%
%%ANKI
Basic
2024-03-06 20:25:34 +00:00
What is initialization of `INSERTION_SORT` 's loop invariant?
2024-02-08 14:02:59 +00:00
Back: Sorting starts with an singleton array which is trivially sorted.
2024-03-19 00:28:34 +00:00
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
2024-02-08 14:02:59 +00:00
<!-- ID: 1707332638373 -->
END%%
%%ANKI
Basic
2024-03-06 20:25:34 +00:00
What is maintenance of `INSERTION_SORT` 's loop invariant?
2024-02-08 14:02:59 +00:00
Back: Each iteration puts the current element into sorted order.
2024-03-19 00:28:34 +00:00
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
2024-02-08 14:02:59 +00:00
<!-- ID: 1707332638375 -->
2024-02-03 03:36:20 +00:00
END%%
2024-02-09 18:50:56 +00:00
%%ANKI
2024-02-15 23:23:53 +00:00
Cloze
2024-03-06 20:25:34 +00:00
`INSERTION_SORT` makes fewer {comparisons} than `SELECTION_SORT` in the average case.
2024-03-19 00:28:34 +00:00
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
2024-02-15 23:23:53 +00:00
<!-- ID: 1708002185982 -->
2024-02-09 18:50:56 +00:00
END%%
2024-02-03 03:36:20 +00:00
## Analogy
Suppose you have a shuffled deck of playing cards face-down on a table. Start by grabbing a card from the deck with your left hand. For the remainder of the cards, use your right hand to transition the topmost card to the end of your left hand. If the newly placed card isn't in sorted order, move it one position closer to the start. Repeat until it's in sorted order.
If you repeat this process for every card in the deck, your left hand will eventually contain the entire deck in sorted order.
%%ANKI
Basic
2024-03-06 20:25:34 +00:00
What analogy does Cormen et al. use to explain `INSERTION_SORT` ?
2024-02-03 03:36:20 +00:00
Back: Sorting a shuffled deck of playing cards.
2024-03-19 00:28:34 +00:00
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
2024-02-03 03:36:20 +00:00
<!-- ID: 1706927594729 -->
END%%
%%ANKI
Basic
2024-03-06 20:25:34 +00:00
What invariant does the left hand maintain in Cormen et al.'s `INSERTION_SORT` analogy?
2024-02-03 03:36:20 +00:00
Back: It contains all drawn cards in sorted order.
2024-03-19 00:28:34 +00:00
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
2024-02-03 03:36:20 +00:00
<!-- ID: 1706927594732 -->
END%%
2024-02-08 14:02:59 +00:00
%%ANKI
Basic
2024-03-06 20:25:34 +00:00
How does `INSERTION_SORT` partition its input array?
2024-02-08 14:02:59 +00:00
Back:
```
[ sorted | unsorted ]
```
2024-03-19 00:28:34 +00:00
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
2024-02-08 14:02:59 +00:00
<!-- ID: 1707399790957 -->
END%%
%%ANKI
Basic
2024-03-06 20:25:34 +00:00
How many comparisons does `INSERTION_SORT` typically perform with `x` ?
2024-02-08 14:02:59 +00:00
```
[ sorted | x : unsorted ]
```
Back: One plus however many elements in `sorted` are greater than `x` .
2024-03-19 00:28:34 +00:00
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
2024-02-08 14:02:59 +00:00
<!-- ID: 1707399790958 -->
END%%
%%ANKI
Basic
2024-03-06 20:25:34 +00:00
Which element will `INSERTION_SORT` move to `sorted` ?
2024-02-08 14:02:59 +00:00
```
[ sorted | unsorted ]
```
Back: The first element of `unsorted` .
2024-03-19 00:28:34 +00:00
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
2024-02-08 14:02:59 +00:00
<!-- ID: 1707399790960 -->
END%%
2024-03-22 15:26:41 +00:00
## Bibliography
2024-02-03 03:36:20 +00:00
2024-03-19 00:28:34 +00:00
* Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).