From 413d48c4d1ba77518f6f19f2fe912b9dbc75ca47 Mon Sep 17 00:00:00 2001 From: Joshua Potter Date: Fri, 2 Feb 2024 07:48:14 -0700 Subject: [PATCH 1/6] Add notes. --- .../plugins/obsidian-to-anki-plugin/data.json | 10 ++-- notes/bash/prompts.md | 45 ++++++++++++++++ notes/bash/shebang.md | 2 +- notes/gawk.md | 51 +++++++++++++++++-- notes/journal/2024-02-02.md | 14 +++++ 5 files changed, 113 insertions(+), 9 deletions(-) create mode 100644 notes/bash/prompts.md create mode 100644 notes/journal/2024-02-02.md diff --git a/notes/.obsidian/plugins/obsidian-to-anki-plugin/data.json b/notes/.obsidian/plugins/obsidian-to-anki-plugin/data.json index 5cbd355..6ae31e0 100644 --- a/notes/.obsidian/plugins/obsidian-to-anki-plugin/data.json +++ b/notes/.obsidian/plugins/obsidian-to-anki-plugin/data.json @@ -64,15 +64,17 @@ "daily/2024-01-31.md": "72e343cef8d56e169cac7b360a88fcf0", "posix/index.md": "f7b1ae55f8f5e8f50f89738b1aca9111", "posix/signals.md": "2120ddd933fc0d57abb93c33f639afd8", - "gawk.md": "4517acfbf4e186e8cc66a9002fbad73f", + "gawk.md": "c79a09e1772da1d4f2cd296b7370ecab", "bash/index.md": "3b5296277f095acdf16655adcdf524af", - "bash/shebang.md": "ad178efeb4a05190b80b5df108c175c7", + "bash/shebang.md": "9006547710f9a079a3666169fbeda7aa", "bash/robustness.md": "de97cd77aae047b5eea27440b43c9c42", - "journal/2024-02-01.md": "f4cc061bfc8e41ce15ae9a354c65ffe9", + "journal/2024-02-01.md": "3aa232387d2dc662384976fd116888eb", "journal/2024-01-31.md": "7c7fbfccabc316f9e676826bf8dfe970", "bash/quoting.md": "b1d8869a91001f8b22f0cdc54d806f61", "nix/callPackage.md": "5ef6bc5d1a549c55d43ebb4d48c64427", - "nix/index.md": "dd5ddd19e95d9bdbe020c68974d77a33" + "nix/index.md": "dd5ddd19e95d9bdbe020c68974d77a33", + "journal/2024-02-02.md": "0df6266a5f347124df5ac795bcff51a2", + "bash/prompts.md": "64bd3cd3c2feb9edb68ad8dc5ba65a35" }, "fields_dict": { "Basic": [ diff --git a/notes/bash/prompts.md b/notes/bash/prompts.md new file mode 100644 index 0000000..0c763be --- /dev/null +++ b/notes/bash/prompts.md @@ -0,0 +1,45 @@ +--- +title: Prompts +TARGET DECK: Obsidian::STEM +FILE TAGS: bash +tags: + - bash +--- + +## Overview + +According to Robbins a POSIX-compliant shell (like Bash) generally has the primary and secondary prompts denoted with `$` and `>` respectively. + +%%ANKI +Basic +What symbol is usually used to denote the primary prompt? +Back: `$$` +Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf) + +END%% + +%%ANKI +Basic +What symbol is usually used to denote the secondary prompt? +Back: `>` +Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf) + +END%% + +Paths supplied to commands are typically "sanitized" by prefixing the path name with `./`. This is mentioned in a few different places: + +* `find -execdir` performs this prefixing automatically on all found files. +* `awk` ambiguously interprets a file named e.g. `count=1` as variable assignment. Should write `$ awk -f program.awk ./count=1`. + +%%ANKI +Basic +What methodology is commonly used to "sanitize" paths supplied as command-line arguments? +Back: Prefixing the paths with `./`. +Reference: Cooper, Mendel. “Advanced Bash-Scripting Guide,” n.d., 916. + +END%% + +## References + +* Cooper, Mendel. “Advanced Bash-Scripting Guide,” n.d., 916. +* Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf) \ No newline at end of file diff --git a/notes/bash/shebang.md b/notes/bash/shebang.md index 25e10f4..0c5c22d 100644 --- a/notes/bash/shebang.md +++ b/notes/bash/shebang.md @@ -8,7 +8,7 @@ tags: ## Overview -The shebang (also writting shabang or sha-bang) is a magic character at the start of a script indicating what command should be run when invoking the script directly. It always begins with ASCII characters `#!`.[^mendel] +The shebang (also writting shabang or sha-bang) is a magic character at the start of a script indicating what command should be run when invoking the script directly. It always begins with ASCII characters `#!`. %%ANKI Basic diff --git a/notes/gawk.md b/notes/gawk.md index 607513a..c3da461 100644 --- a/notes/gawk.md +++ b/notes/gawk.md @@ -40,7 +40,7 @@ END%% Robbins suggests executing command `set +H` on [[bash]] startup to disable [[C]] shell-style command history. -## Usage +## Structure `awk` applies actions to lines matching specified patterns. In this way `awk` is said to be data-driven - we specify the lines `awk` should act on and `awk` is responsible for finding and acting on them. Instructions are provided via a **program**. @@ -88,7 +88,7 @@ Basic Write the `awk` command that searches file `mail-list` for string `li`. Back: ```bash -$ awk '/li/ { print }' mail-list +$ awk '/li/' mail-list ``` Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf) @@ -141,7 +141,7 @@ END%% %%ANKI Basic -What is the `BEGIN` pattern? +How is the `BEGIN` pattern interpreted? Back: Code associated with it executes before any input is read. Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf) @@ -149,7 +149,7 @@ END%% %%ANKI Basic -What is the `END` pattern? +How is the `END` pattern interpreted? Back: Code associated with it executes after all input has been read. Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf) @@ -188,6 +188,49 @@ Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 202 END%% +`awk` is said to be a "line-oriented" language. Every rule's action must begin on the same line as the pattern. + +%%ANKI +Basic +When can a rule's pattern and action exist on different lines? +Back: Only when using backslash continuation. +Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf) + +END%% + +## Variables + +Variables are defined like `var=val`. They can be specified in two different places: + +1. Via the `-v` command line flag. Using this allows accessing the variable value from within a `BEGIN` rule. +2. In the file list. Using this allows accessing the variable value in all subsequent file processing. + +%%ANKI +Basic +Where in an `awk` invocation can variables be assigned? +Back: As a `-v` argument or in the file list. +Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf) + +END%% + +%%ANKI +Basic +The `-v` flag was introduced to accommodate what functionality? +Back: Accessing variables from a `BEGIN` rule. +Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf) + +END%% + +%%ANKI +Basic +Describe what the following command does in in a single sentence: +```bash +$ awk 'program' pass=1 data pass=2 data +``` +Back: Evaluates `'program'` against the `data` file twice with a different value of `pass` on each run. +Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf) + +END%% ## References * Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf) \ No newline at end of file diff --git a/notes/journal/2024-02-02.md b/notes/journal/2024-02-02.md new file mode 100644 index 0000000..2373ead --- /dev/null +++ b/notes/journal/2024-02-02.md @@ -0,0 +1,14 @@ +--- +title: "2024-02-02" +--- + +- [x] Anki Flashcards +- [x] KoL +- [x] Sheet Music (10 min.) +- [x] OGS (1 Life & Death Problem) +- [x] Korean (Read 1 Story) +- [ ] Interview Prep (1 Practice Problem) +- [ ] Log Work Hours (Max 3 hours) + +* Read 효자 호랑이 (The Filial Tiger). +* Read through [Project #1 - Buffer Pool](https://15445.courses.cs.cmu.edu/fall2022/project1/) in anticipation of my call with Kevin later. \ No newline at end of file -- 2.42.0 From 0490802b9146f60b23d491389840f7473e42b71e Mon Sep 17 00:00:00 2001 From: Joshua Potter Date: Fri, 2 Feb 2024 11:12:18 -0700 Subject: [PATCH 2/6] Add more notes around zotero/interview practice. --- notes/journal/2024-02-02.md | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/notes/journal/2024-02-02.md b/notes/journal/2024-02-02.md index 2373ead..7684eb9 100644 --- a/notes/journal/2024-02-02.md +++ b/notes/journal/2024-02-02.md @@ -7,8 +7,13 @@ title: "2024-02-02" - [x] Sheet Music (10 min.) - [x] OGS (1 Life & Death Problem) - [x] Korean (Read 1 Story) -- [ ] Interview Prep (1 Practice Problem) +- [x] Interview Prep (1 Practice Problem) - [ ] Log Work Hours (Max 3 hours) * Read 효자 호랑이 (The Filial Tiger). -* Read through [Project #1 - Buffer Pool](https://15445.courses.cs.cmu.edu/fall2022/project1/) in anticipation of my call with Kevin later. \ No newline at end of file +* Read through [Project #1 - Buffer Pool](https://15445.courses.cs.cmu.edu/fall2022/project1/) in anticipation of my call with Kevin later. + * Call was pushed back to next week. +* Upgraded to Zotero 7. It's a beta version but already has some nice quality of life improvements I want to take advantage of. + * Dark mode. + * 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. \ No newline at end of file -- 2.42.0 From d744ce4ebca07fd05c5090ec70fb33eaa146b367 Mon Sep 17 00:00:00 2001 From: Joshua Potter Date: Fri, 2 Feb 2024 15:38:11 -0700 Subject: [PATCH 3/6] Add sorting concepts. --- .../plugins/obsidian-to-anki-plugin/data.json | 7 ++- notes/algorithms/index.md | 3 ++ notes/algorithms/sorting/index.md | 54 +++++++++++++++++++ notes/algorithms/sorting/insertion-sort.md | 22 ++++++++ 4 files changed, 84 insertions(+), 2 deletions(-) create mode 100644 notes/algorithms/index.md create mode 100644 notes/algorithms/sorting/index.md create mode 100644 notes/algorithms/sorting/insertion-sort.md diff --git a/notes/.obsidian/plugins/obsidian-to-anki-plugin/data.json b/notes/.obsidian/plugins/obsidian-to-anki-plugin/data.json index 6ae31e0..258ac52 100644 --- a/notes/.obsidian/plugins/obsidian-to-anki-plugin/data.json +++ b/notes/.obsidian/plugins/obsidian-to-anki-plugin/data.json @@ -73,8 +73,11 @@ "bash/quoting.md": "b1d8869a91001f8b22f0cdc54d806f61", "nix/callPackage.md": "5ef6bc5d1a549c55d43ebb4d48c64427", "nix/index.md": "dd5ddd19e95d9bdbe020c68974d77a33", - "journal/2024-02-02.md": "0df6266a5f347124df5ac795bcff51a2", - "bash/prompts.md": "64bd3cd3c2feb9edb68ad8dc5ba65a35" + "journal/2024-02-02.md": "e2acbe75752d9c39875553223e34fb0d", + "bash/prompts.md": "64bd3cd3c2feb9edb68ad8dc5ba65a35", + "algorithms/sorting/index.md": "9aedfae96c9bb86fcba6afd2800538ae", + "algorithms/sorting/insertion-sort.md": "0bdccffe868d40986aa7d0d49da918f3", + "algorithms/index.md": "1583c07edea4736db27c38fe2b6c4c31" }, "fields_dict": { "Basic": [ diff --git a/notes/algorithms/index.md b/notes/algorithms/index.md new file mode 100644 index 0000000..889c7d7 --- /dev/null +++ b/notes/algorithms/index.md @@ -0,0 +1,3 @@ +--- +title: Algorithms +--- diff --git a/notes/algorithms/sorting/index.md b/notes/algorithms/sorting/index.md new file mode 100644 index 0000000..cdfb2c3 --- /dev/null +++ b/notes/algorithms/sorting/index.md @@ -0,0 +1,54 @@ +--- +title: Sorting +TARGET DECK: Obsidian::STEM +FILE TAGS: algorithm sorting +tags: + - algorithm + - sorting +--- + +## Overview + +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'$. + +## 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. + +```elixir +iex> 1 < :atom # structural +true +iex> Date.compare(~D[2017-03-31], ~D[2017-04-01]) # semantic +:lt +``` + +%%ANKI +Basic +What are the two types of comparisons made between data types? +Back: Structural and semantic. +Reference: “Kernel — Elixir v1.16.1,” accessed February 2, 2024, [https://hexdocs.pm/elixir/1.16/Kernel.html#module-structural-comparison](https://hexdocs.pm/elixir/1.16/Kernel.html#module-structural-comparison). + +END%% + +%%ANKI +Basic +What is structural comparison of two data types? +Back: Comparison of the underlying data structures making up data types. +Reference: “Kernel — Elixir v1.16.1,” accessed February 2, 2024, [https://hexdocs.pm/elixir/1.16/Kernel.html#module-structural-comparison](https://hexdocs.pm/elixir/1.16/Kernel.html#module-structural-comparison). + +END%% + +%%ANKI +Basic +What is semantic comparison of two data types? +Back: Comparison made with respect to what the data types represent. +Reference: “Kernel — Elixir v1.16.1,” accessed February 2, 2024, [https://hexdocs.pm/elixir/1.16/Kernel.html#module-structural-comparison](https://hexdocs.pm/elixir/1.16/Kernel.html#module-structural-comparison). + +END%% + +## References + +* Thomas H. Cormen et al., _Introduction to Algorithms_, 3rd ed (Cambridge, Mass: MIT Press, 2009). +* “Kernel — Elixir v1.16.1,” accessed February 2, 2024, [https://hexdocs.pm/elixir/1.16/Kernel.html#module-structural-comparison](https://hexdocs.pm/elixir/1.16/Kernel.html#module-structural-comparison). + +[^structural]: [Structural Comparison](https://hexdocs.pm/elixir/1.16/Kernel.html#module-structural-comparison) \ No newline at end of file diff --git a/notes/algorithms/sorting/insertion-sort.md b/notes/algorithms/sorting/insertion-sort.md new file mode 100644 index 0000000..b99a6d8 --- /dev/null +++ b/notes/algorithms/sorting/insertion-sort.md @@ -0,0 +1,22 @@ +--- +title: Insertion Sort +TARGET DECK: Obsidian::STEM +FILE TAGS: algorithm sorting +tags: + - algorithm + - sorting +--- + +## Overview + +| Property | Value | +| ------------- | ---------- | +| Best Case || +| Worst Case || +| Average Case || +| Memory || +| In place? || + +## References + +* Thomas H. Cormen et al., _Introduction to Algorithms_, 3rd ed (Cambridge, Mass: MIT Press, 2009). \ No newline at end of file -- 2.42.0 From 21517314a4fb32ec823536a6fab53e4467c0dac1 Mon Sep 17 00:00:00 2001 From: Joshua Potter Date: Fri, 2 Feb 2024 18:22:08 -0700 Subject: [PATCH 4/6] Add insertion sort notes. --- notes/algorithms/sorting/insertion-sort.md | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/notes/algorithms/sorting/insertion-sort.md b/notes/algorithms/sorting/insertion-sort.md index b99a6d8..6b63016 100644 --- a/notes/algorithms/sorting/insertion-sort.md +++ b/notes/algorithms/sorting/insertion-sort.md @@ -15,7 +15,24 @@ tags: | Worst Case || | Average Case || | Memory || -| In place? || +| In place || +| Stable || + +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`: + +* `Q`: `i = 1` +* `R`: `i = n` and `A[1..n]` is in sorted order + +Next establish loop invariant `P` and bounds function `t`: + +* `P`: `1 \leq i \leq n` and `A[1..i]` is in sorted order +* `t`: the number of inversions of `A[1..i]` + +## 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. ## References -- 2.42.0 From 48766eccb0537f5f93c7e283302d68a46d24502f Mon Sep 17 00:00:00 2001 From: Joshua Potter Date: Fri, 2 Feb 2024 19:34:38 -0700 Subject: [PATCH 5/6] Add details on insertion sort. --- .../plugins/obsidian-to-anki-plugin/data.json | 4 +- notes/algorithms/sorting/index.md | 15 +++ notes/algorithms/sorting/insertion-sort.md | 107 +++++++++++++++--- 3 files changed, 109 insertions(+), 17 deletions(-) diff --git a/notes/.obsidian/plugins/obsidian-to-anki-plugin/data.json b/notes/.obsidian/plugins/obsidian-to-anki-plugin/data.json index 258ac52..de477aa 100644 --- a/notes/.obsidian/plugins/obsidian-to-anki-plugin/data.json +++ b/notes/.obsidian/plugins/obsidian-to-anki-plugin/data.json @@ -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": { diff --git a/notes/algorithms/sorting/index.md b/notes/algorithms/sorting/index.md index cdfb2c3..51d8b6c 100644 --- a/notes/algorithms/sorting/index.md +++ b/notes/algorithms/sorting/index.md @@ -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). + +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). + +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. diff --git a/notes/algorithms/sorting/insertion-sort.md b/notes/algorithms/sorting/insertion-sort.md index 6b63016..63762a2 100644 --- a/notes/algorithms/sorting/insertion-sort.md +++ b/notes/algorithms/sorting/insertion-sort.md @@ -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). + +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). + +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). + +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). + +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). + +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). + +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). + +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). + +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). + +END%% + ## References -* Thomas H. Cormen et al., _Introduction to Algorithms_, 3rd ed (Cambridge, Mass: MIT Press, 2009). \ No newline at end of file +* Thomas H. Cormen et al., _Introduction to Algorithms_, 3rd ed (Cambridge, Mass: MIT Press, 2009). -- 2.42.0 From eb1fd15f82db077be88efda8fe663a2a2c6ef240 Mon Sep 17 00:00:00 2001 From: Joshua Potter Date: Fri, 2 Feb 2024 19:36:20 -0700 Subject: [PATCH 6/6] Journal update. --- notes/journal/2024-02-02.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/notes/journal/2024-02-02.md b/notes/journal/2024-02-02.md index 7684eb9..18d17c4 100644 --- a/notes/journal/2024-02-02.md +++ b/notes/journal/2024-02-02.md @@ -16,4 +16,6 @@ title: "2024-02-02" * Upgraded to Zotero 7. It's a beta version but already has some nice quality of life improvements I want to take advantage of. * Dark mode. * 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. \ No newline at end of file +* 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. \ No newline at end of file -- 2.42.0