Compare commits

...

2 Commits

Author SHA1 Message Date
Joshua Potter eb1fd15f82 Journal update. 2024-02-02 19:36:20 -07:00
Joshua Potter 48766eccb0 Add details on insertion sort. 2024-02-02 19:34:38 -07:00
4 changed files with 112 additions and 18 deletions

View File

@ -75,8 +75,8 @@
"nix/index.md": "dd5ddd19e95d9bdbe020c68974d77a33", "nix/index.md": "dd5ddd19e95d9bdbe020c68974d77a33",
"journal/2024-02-02.md": "e2acbe75752d9c39875553223e34fb0d", "journal/2024-02-02.md": "e2acbe75752d9c39875553223e34fb0d",
"bash/prompts.md": "64bd3cd3c2feb9edb68ad8dc5ba65a35", "bash/prompts.md": "64bd3cd3c2feb9edb68ad8dc5ba65a35",
"algorithms/sorting/index.md": "9aedfae96c9bb86fcba6afd2800538ae", "algorithms/sorting/index.md": "cd189e1a2cf32b5656b16aaf9f488874",
"algorithms/sorting/insertion-sort.md": "0bdccffe868d40986aa7d0d49da918f3", "algorithms/sorting/insertion-sort.md": "c78c9983f87cdc4198f82803d418967f",
"algorithms/index.md": "1583c07edea4736db27c38fe2b6c4c31" "algorithms/index.md": "1583c07edea4736db27c38fe2b6c4c31"
}, },
"fields_dict": { "fields_dict": {

View File

@ -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'$. 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 ## 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. 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.

View File

@ -9,24 +9,85 @@ tags:
## Overview ## Overview
| Property | Value | | Property | Value |
| ------------- | ---------- | | ---------- | -------- |
| Best Case || | Best Case | $O(n)$ |
| Worst Case || | Worst Case | $O(n^2)$ |
| Average Case || | Avg. Case | $O(n^2)$ |
| Memory || | Memory | $O(1)$ |
| In place || | In place | Yes |
| Stable || | 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` %%ANKI
* `R`: `i = n` and `A[1..n]` is in sorted order 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 %%ANKI
* `t`: the number of inversions of `A[1..i]` 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 ## 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. 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 ## 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).

View File

@ -17,3 +17,5 @@ title: "2024-02-02"
* Dark mode. * Dark mode.
* Ability to comment on snapshots. * Ability to comment on snapshots.
* Answered [String to Integer (atoi)](https://leetcode.com/problems/string-to-integer-atoi/description/). Admittedly though I floundered around. I should try and think more critically at each step instead of getting impatient like I did. * Answered [String to Integer (atoi)](https://leetcode.com/problems/string-to-integer-atoi/description/). Admittedly though I floundered around. I should try and think more critically at each step instead of getting impatient like I did.
* More reading on `awk`. This time around variables.
* Begin deep dive into insertion sort. Starting off with the basics, but want to think much more broadly on the role of insertion sort throughout computing.