Add details on insertion sort.
parent
21517314a4
commit
48766eccb0
|
@ -75,8 +75,8 @@
|
|||
"nix/index.md": "dd5ddd19e95d9bdbe020c68974d77a33",
|
||||
"journal/2024-02-02.md": "e2acbe75752d9c39875553223e34fb0d",
|
||||
"bash/prompts.md": "64bd3cd3c2feb9edb68ad8dc5ba65a35",
|
||||
"algorithms/sorting/index.md": "9aedfae96c9bb86fcba6afd2800538ae",
|
||||
"algorithms/sorting/insertion-sort.md": "0bdccffe868d40986aa7d0d49da918f3",
|
||||
"algorithms/sorting/index.md": "cd189e1a2cf32b5656b16aaf9f488874",
|
||||
"algorithms/sorting/insertion-sort.md": "c78c9983f87cdc4198f82803d418967f",
|
||||
"algorithms/index.md": "1583c07edea4736db27c38fe2b6c4c31"
|
||||
},
|
||||
"fields_dict": {
|
||||
|
|
|
@ -11,6 +11,21 @@ tags:
|
|||
|
||||
Let $n \geq 0$ and $S = \langle a_1, a_2, \ldots, a_n \rangle$ be a sequence. The **sorting problem** refers to permuting **keys** $a_1, a_2, \ldots, a_n$ into a new sequence $\langle a_1', a_2', \ldots, a_n' \rangle$ such that $a_1' \leq a_2' \leq \cdots \leq a_n'$.
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
What makes a sorting algorithm stable?
|
||||
Back: "Equal" values are ordered the same in the output as they are in the input.
|
||||
Reference: Thomas H. Cormen et al., _Introduction to Algorithms_, 3rd ed (Cambridge, Mass: MIT Press, 2009).
|
||||
<!--ID: 1706925787139-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
What is an in place sorting algorithm?
|
||||
Back: One in which only a constant number of input values are ever stored outside the array.
|
||||
Reference: Thomas H. Cormen et al., _Introduction to Algorithms_, 3rd ed (Cambridge, Mass: MIT Press, 2009).
|
||||
<!--ID: 1706925787146-->
|
||||
END%%
|
||||
## Structural Comparison
|
||||
|
||||
The #Elixir documentation makes a point that there exist two types of comparisons between data types.[^structural] The first is **structural** in which comparisons are made on the underlying data structures used to describe the data types. The second is **semantic** which focuses on making the comparison with respect to what the data types represent.
|
||||
|
|
|
@ -9,24 +9,85 @@ tags:
|
|||
|
||||
## Overview
|
||||
|
||||
| Property | Value |
|
||||
| ------------- | ---------- |
|
||||
| Best Case ||
|
||||
| Worst Case ||
|
||||
| Average Case ||
|
||||
| Memory ||
|
||||
| In place ||
|
||||
| Stable ||
|
||||
| Property | Value |
|
||||
| ---------- | -------- |
|
||||
| Best Case | $O(n)$ |
|
||||
| Worst Case | $O(n^2)$ |
|
||||
| Avg. Case | $O(n^2)$ |
|
||||
| Memory | $O(1)$ |
|
||||
| In place | Yes |
|
||||
| Stable | Yes |
|
||||
|
||||
Insertion sort works by advancing an index `i` through an array `A[1..n]` such that `A[1..i]` is put into sorted order. Consider precondition `Q` and postcondition `R`:
|
||||
Insertion sort works by advancing an index `i` through an array `A[1..n]` such that `A[1..i]` is kept in sorted order.
|
||||
|
||||
* `Q`: `i = 1`
|
||||
* `R`: `i = n` and `A[1..n]` is in sorted order
|
||||
%%ANKI
|
||||
Basic
|
||||
What is insertion sort's best case runtime?
|
||||
Back: $O(n)$
|
||||
Reference: Thomas H. Cormen et al., _Introduction to Algorithms_, 3rd ed (Cambridge, Mass: MIT Press, 2009).
|
||||
<!--ID: 1706925879541-->
|
||||
END%%
|
||||
|
||||
Next establish loop invariant `P` and bounds function `t`:
|
||||
%%ANKI
|
||||
Basic
|
||||
What input value does insertion sort perform best on?
|
||||
Back: An already sorted array.
|
||||
Reference: Thomas H. Cormen et al., _Introduction to Algorithms_, 3rd ed (Cambridge, Mass: MIT Press, 2009).
|
||||
<!--ID: 1706925921544-->
|
||||
END%%
|
||||
|
||||
* `P`: `1 \leq i \leq n` and `A[1..i]` is in sorted order
|
||||
* `t`: the number of inversions of `A[1..i]`
|
||||
%%ANKI
|
||||
Basic
|
||||
What is insertion sort's worst case runtime?
|
||||
Back: $O(n^2)$
|
||||
Reference: Thomas H. Cormen et al., _Introduction to Algorithms_, 3rd ed (Cambridge, Mass: MIT Press, 2009).
|
||||
<!--ID: 1706926586947-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
What input value does insertion sort perform worst on?
|
||||
Back: An array in reverse-sorted order.
|
||||
Reference: Thomas H. Cormen et al., _Introduction to Algorithms_, 3rd ed (Cambridge, Mass: MIT Press, 2009).
|
||||
<!--ID: 1706926586951-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
Is insertion sort in place?
|
||||
Back: Yes
|
||||
Reference: Thomas H. Cormen et al., _Introduction to Algorithms_, 3rd ed (Cambridge, Mass: MIT Press, 2009).
|
||||
<!--ID: 1706926586955-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
Is insertion sort stable?
|
||||
Back: Yes
|
||||
Reference: Thomas H. Cormen et al., _Introduction to Algorithms_, 3rd ed (Cambridge, Mass: MIT Press, 2009).
|
||||
<!--ID: 1706926586959-->
|
||||
END%%
|
||||
|
||||
```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
|
||||
What loop invariant is maintained in insertion sort?
|
||||
Back: `A[1..i]` is in sorted order.
|
||||
Reference: Thomas H. Cormen et al., _Introduction to Algorithms_, 3rd ed (Cambridge, Mass: MIT Press, 2009).
|
||||
<!--ID: 1706927594718-->
|
||||
END%%
|
||||
|
||||
## Analogy
|
||||
|
||||
|
@ -34,6 +95,22 @@ Suppose you have a shuffled deck of playing cards face-down on a table. Start by
|
|||
|
||||
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
|
||||
What analogy does Cormen et al. use to explain insertion sort?
|
||||
Back: Sorting a shuffled deck of playing cards.
|
||||
Reference: Thomas H. Cormen et al., _Introduction to Algorithms_, 3rd ed (Cambridge, Mass: MIT Press, 2009).
|
||||
<!--ID: 1706927594729-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
What invariant does the left hand maintain in Cormen et al.'s insertion sort analogy?
|
||||
Back: It contains all drawn cards in sorted order.
|
||||
Reference: Thomas H. Cormen et al., _Introduction to Algorithms_, 3rd ed (Cambridge, Mass: MIT Press, 2009).
|
||||
<!--ID: 1706927594732-->
|
||||
END%%
|
||||
|
||||
## References
|
||||
|
||||
* Thomas H. Cormen et al., _Introduction to Algorithms_, 3rd ed (Cambridge, Mass: MIT Press, 2009).
|
||||
* Thomas H. Cormen et al., _Introduction to Algorithms_, 3rd ed (Cambridge, Mass: MIT Press, 2009).
|
||||
|
|
Loading…
Reference in New Issue