Add merge sort and more notes on floors/ceilings.

c-declarations
Joshua Potter 2024-02-23 19:43:29 -07:00
parent c1a19058ef
commit 2663b97c55
6 changed files with 296 additions and 7 deletions

View File

@ -79,10 +79,11 @@
"lattice-path-chessboard.png", "lattice-path-chessboard.png",
"lattice-path-chessboard-colored.png", "lattice-path-chessboard-colored.png",
"lattice-path-before-recurrence.png", "lattice-path-before-recurrence.png",
"lattice-path-after-recurrence.png" "lattice-path-after-recurrence.png",
"merge-sort.gif"
], ],
"File Hashes": { "File Hashes": {
"algorithms/index.md": "a5ff7313f71777f1f3536e27dd9894fa", "algorithms/index.md": "cd7c7ba91fb2f961c9f2437777e8e2ac",
"algorithms/sorting/index.md": "2d5a18a3079d96fa9e3d4289181a8b6c", "algorithms/sorting/index.md": "2d5a18a3079d96fa9e3d4289181a8b6c",
"algorithms/sorting/insertion-sort.md": "00e4edb132d473b0516fde3307ebae30", "algorithms/sorting/insertion-sort.md": "00e4edb132d473b0516fde3307ebae30",
"bash/index.md": "3dfeb538d781e4645e3aaaf32beb1034", "bash/index.md": "3dfeb538d781e4645e3aaaf32beb1034",
@ -166,7 +167,7 @@
"_journal/2024-02/2024-02-14.md": "aa009f9569e175a8104b0537ebcc5520", "_journal/2024-02/2024-02-14.md": "aa009f9569e175a8104b0537ebcc5520",
"_journal/2024-02-16.md": "5cc129254afd553829be3364facd23db", "_journal/2024-02-16.md": "5cc129254afd553829be3364facd23db",
"_journal/2024-02/2024-02-15.md": "16cb7563d404cb543719b7bb5037aeed", "_journal/2024-02/2024-02-15.md": "16cb7563d404cb543719b7bb5037aeed",
"algebra/floor-ceiling.md": "e9f905f5ec01921e0f19afe8732fb38b", "algebra/floor-ceiling.md": "828b11e642d6803a740b58f27ff047cf",
"algebra/index.md": "90b842eb694938d87c7c68779a5cacd1", "algebra/index.md": "90b842eb694938d87c7c68779a5cacd1",
"algorithms/binary-search.md": "08cb6dc2dfb204a665d8e8333def20ca", "algorithms/binary-search.md": "08cb6dc2dfb204a665d8e8333def20ca",
"_journal/2024-02-17.md": "7c37cb10515ed3d2f5388eaf02a67048", "_journal/2024-02-17.md": "7c37cb10515ed3d2f5388eaf02a67048",
@ -189,12 +190,13 @@
"algebra/radices.md": "0fcd901c798eaed8075ff1375e2429dd", "algebra/radices.md": "0fcd901c798eaed8075ff1375e2429dd",
"_journal/2024-02-22.md": "e01f1d4bd2f7ac2a667cdfd500885a2a", "_journal/2024-02-22.md": "e01f1d4bd2f7ac2a667cdfd500885a2a",
"_journal/2024-02/2024-02-21.md": "f423137ae550eb958378750d1f5e98c7", "_journal/2024-02/2024-02-21.md": "f423137ae550eb958378750d1f5e98c7",
"_journal/2024-02-23.md": "75eec3feffa90a219de77151771fe3fe", "_journal/2024-02-23.md": "219ce9ad15a8733edd476c97628b71fd",
"_journal/2024-02/2024-02-22.md": "312e55d57868026f6e80f7989a889c2b", "_journal/2024-02/2024-02-22.md": "312e55d57868026f6e80f7989a889c2b",
"c17/strings.md": "bbe8983602adbeb38eff214beddedd84", "c17/strings.md": "bbe8983602adbeb38eff214beddedd84",
"c17/index.md": "78576ee41d0185df82c59999142f4edb", "c17/index.md": "78576ee41d0185df82c59999142f4edb",
"c17/escape-sequences.md": "ebc63c6cdfbe60bbc2708c1b0c8da8bb", "c17/escape-sequences.md": "ebc63c6cdfbe60bbc2708c1b0c8da8bb",
"c17/declarations.md": "46b135d583a992991c889d518fec1c0f" "c17/declarations.md": "46b135d583a992991c889d518fec1c0f",
"algorithms/sorting/merge-sort.md": "f66f482e5bd551c765fcba564c938d67"
}, },
"fields_dict": { "fields_dict": {
"Basic": [ "Basic": [

View File

@ -7,7 +7,7 @@ title: "2024-02-23"
- [ ] Sheet Music (10 min.) - [ ] Sheet Music (10 min.)
- [x] OGS (1 Life & Death Problem) - [x] OGS (1 Life & Death Problem)
- [ ] Korean (Read 1 Story) - [ ] Korean (Read 1 Story)
- [ ] Interview Prep (1 Practice Problem) - [x] Interview Prep (1 Practice Problem)
- [x] Log Work Hours (Max 3 hours) - [x] Log Work Hours (Max 3 hours)
* 101weiqi (serial numbers) * 101weiqi (serial numbers)
@ -19,4 +19,6 @@ title: "2024-02-23"
* Q-9107 * Q-9107
* Read about extension and truncation of integral values using unsigned and two's-complement encoding. * Read about extension and truncation of integral values using unsigned and two's-complement encoding.
* Read through last sections of "Discrete Mathematics: An Open Introduction"'s first chapter. * Read through last sections of "Discrete Mathematics: An Open Introduction"'s first chapter.
* I took a few little notes but I didn't pay as close attention to these sections as others. * I took a few notes but didn't pay as close attention to these sections as others.
* Leetcode [Decode Ways](https://leetcode.com/problems/decode-ways/solutions/).
* Practiced implementing heap sort and quicksort.

View File

@ -240,6 +240,22 @@ Reference: Ronald L. Graham, Donald Ervin Knuth, and Oren Patashnik, *Concrete M
<!--ID: 1708115683351--> <!--ID: 1708115683351-->
END%% END%%
%%ANKI
Basic
Given `A[p..q]` and $r = \lfloor (p + q) / 2 \rfloor$, what is the size of `A[p..r]` in terms of $n = q - p + 1$?
Back: $\lceil n / 2 \rceil$.
Reference: Thomas H. Cormen et al., *Introduction to Algorithms*, 3rd ed (Cambridge, Mass: MIT Press, 2009).
<!--ID: 1708742467192-->
END%%
%%ANKI
Basic
Given `A[p..q]` and $r = \lfloor (p + q) / 2 \rfloor$, what is the size of `A[r+1..q]` in terms of $n = q - p + 1$?
Back: $\lfloor n / 2 \rfloor$.
Reference: Thomas H. Cormen et al., *Introduction to Algorithms*, 3rd ed (Cambridge, Mass: MIT Press, 2009).
<!--ID: 1708742467198-->
END%%
%%ANKI %%ANKI
Basic Basic
Given `A[p..q]` and $r = \lceil (p + q) / 2 \rceil$, how does the size of `A[p..r-1]` compare to `A[r..q]`? Given `A[p..q]` and $r = \lceil (p + q) / 2 \rceil$, how does the size of `A[p..r-1]` compare to `A[r..q]`?
@ -248,6 +264,22 @@ Reference: Ronald L. Graham, Donald Ervin Knuth, and Oren Patashnik, *Concrete M
<!--ID: 1708115683354--> <!--ID: 1708115683354-->
END%% END%%
%%ANKI
Basic
Given `A[p..q]` and $r = \lceil (p + q) / 2 \rceil$, what is the size of `A[r..q]` in terms of $n = q - p + 1$?
Back: $\lceil n / 2 \rceil$.
Reference: Thomas H. Cormen et al., *Introduction to Algorithms*, 3rd ed (Cambridge, Mass: MIT Press, 2009).
<!--ID: 1708742467202-->
END%%
%%ANKI
Basic
Given `A[p..q]` and $r = \lceil (p + q) / 2 \rceil$, what is the size of `A[p..r-1]` in terms of $n = q - p + 1$?
Back: $\lfloor n / 2 \rfloor$.
Reference: Thomas H. Cormen et al., *Introduction to Algorithms*, 3rd ed (Cambridge, Mass: MIT Press, 2009).
<!--ID: 1708742467207-->
END%%
%%ANKI %%ANKI
Basic Basic
Given `A[p..q]` and $r = \lfloor (p + q) / 2 \rfloor$, how does the size of `A[p..r-1]` compare to `A[r..q]`? Given `A[p..q]` and $r = \lfloor (p + q) / 2 \rfloor$, how does the size of `A[p..r-1]` compare to `A[r..q]`?

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.7 KiB

View File

@ -1,5 +1,102 @@
--- ---
title: Algorithms title: Algorithms
TARGET DECK: Obsidian::STEM
FILE TAGS: algorithm
tags: tags:
- algorithm - algorithm
--- ---
## Overview
An **incremental** approach to algorithm design involves acting on a single element at a time. In contrast, the **divide-and-conquer** approach breaks problems into subproblems that are easier to solve.
%%ANKI
Basic
What does an incremental approach to algorithm design refer to?
Back: An algorithm that acts on a single element at a time.
Reference: Thomas H. Cormen et al., *Introduction to Algorithms*, 3rd ed (Cambridge, Mass: MIT Press, 2009).
<!--ID: 1708742467144-->
END%%
%%ANKI
Basic
What does a divide-and-conquer approach to algorithm design refer to?
Back: An algorithm that breaks a problem into similar but simpler subproblems.
Reference: Thomas H. Cormen et al., *Introduction to Algorithms*, 3rd ed (Cambridge, Mass: MIT Press, 2009).
<!--ID: 1708742467147-->
END%%
%%ANKI
Basic
What does it mean for a divide-and-conquer algorithm to "bottom out"?
Back: An input that cannot (or should not) be divided any further is encountered.
Reference: Thomas H. Cormen et al., *Introduction to Algorithms*, 3rd ed (Cambridge, Mass: MIT Press, 2009).
<!--ID: 1708742467151-->
END%%
%%ANKI
Basic
In the context of algorithms, what does a "sentinel" refer to?
Back: A special value used to simplify code.
Reference: Thomas H. Cormen et al., *Introduction to Algorithms*, 3rd ed (Cambridge, Mass: MIT Press, 2009).
<!--ID: 1708742467155-->
END%%
%%ANKI
Cloze
Insertion sort is to an {incremental} design approach whereas merge sort is to a {divide-and-conquer} design approach.
Reference: Thomas H. Cormen et al., *Introduction to Algorithms*, 3rd ed (Cambridge, Mass: MIT Press, 2009).
<!--ID: 1708742467159-->
END%%
%%ANKI
Basic
What ideas does the term "divide-and-conquer" invoke?
Back: Breaking a problem into subproblems that are easier to solve.
Reference: Thomas H. Cormen et al., *Introduction to Algorithms*, 3rd ed (Cambridge, Mass: MIT Press, 2009).
<!--ID: 1708742467164-->
END%%
%%ANKI
Basic
According to Cormen et al., what three steps do divide-and-conquer algorithms take?
Back: Divide, conquer, and combine.
Reference: Thomas H. Cormen et al., *Introduction to Algorithms*, 3rd ed (Cambridge, Mass: MIT Press, 2009).
<!--ID: 1708742467167-->
END%%
%%ANKI
Basic
What is the "divide" step of a divide-and-conquer algorithm?
Back: Breaking the problem into smaller instances of the same problem.
Reference: Thomas H. Cormen et al., *Introduction to Algorithms*, 3rd ed (Cambridge, Mass: MIT Press, 2009).
<!--ID: 1708742467172-->
END%%
%%ANKI
Basic
What is the "conquer" step of a divide-and-conquer algorithm?
Back: Solving subproblems recursively or, if small enough, in a straightforward manner.
Reference: Thomas H. Cormen et al., *Introduction to Algorithms*, 3rd ed (Cambridge, Mass: MIT Press, 2009).
<!--ID: 1708742467178-->
END%%
%%ANKI
Basic
What is the "combine" step of a divide-and-conquer algorithm?
Back: Manipulating solutions to smaller problems into a solution for the original problem.
Reference: Thomas H. Cormen et al., *Introduction to Algorithms*, 3rd ed (Cambridge, Mass: MIT Press, 2009).
<!--ID: 1708742467182-->
END%%
%%ANKI
Basic
What is a running time recurrence?
Back: A formula that describes overall running time in terms of running time on smaller inputs.
Reference: Thomas H. Cormen et al., *Introduction to Algorithms*, 3rd ed (Cambridge, Mass: MIT Press, 2009).
<!--ID: 1708742467187-->
END%%
## References
* Thomas H. Cormen et al., *Introduction to Algorithms*, 3rd ed (Cambridge, Mass: MIT Press, 2009).

View File

@ -0,0 +1,156 @@
---
title: Merge Sort
TARGET DECK: Obsidian::STEM
FILE TAGS: algorithm::sorting
tags:
- algorithm
- sorting
---
## Overview
Property | Value
----------- | --------
Best Case | $\Omega(n\lg{n})$
Worst Case | $O(n\lg{n})$
Avg. Case | $O(n\lg{n})$
Aux. Memory | -
Stable | -
Adaptive | -
![[merge-sort.gif]]
%%ANKI
Basic
What does the term "merge" in merge sort refer to?
Back: The primary operation used to combine array halves.
Reference: Thomas H. Cormen et al., *Introduction to Algorithms*, 3rd ed (Cambridge, Mass: MIT Press, 2009).
<!--ID: 1708742467101-->
END%%
%%ANKI
Basic
What is merge sort's best case runtime?
Back: $\Omega(n\lg{n})$
Reference: Thomas H. Cormen et al., *Introduction to Algorithms*, 3rd ed (Cambridge, Mass: MIT Press, 2009).
<!--ID: 1708742467109-->
END%%
%%ANKI
Basic
What is merge sort's worst case runtime?
Back: $O(n\lg{n})$
Reference: Thomas H. Cormen et al., *Introduction to Algorithms*, 3rd ed (Cambridge, Mass: MIT Press, 2009).
<!--ID: 1708742467112-->
END%%
%%ANKI
Basic
What is merge sort's average case runtime?
Back: $O(n\lg{n})$
Reference: Thomas H. Cormen et al., *Introduction to Algorithms*, 3rd ed (Cambridge, Mass: MIT Press, 2009).
<!--ID: 1708742467115-->
END%%
%%ANKI
Basic
What is the "divide" step of merge sort?
Back: Divide the input array into two subarrays of half size.
Reference: Thomas H. Cormen et al., *Introduction to Algorithms*, 3rd ed (Cambridge, Mass: MIT Press, 2009).
<!--ID: 1708742467118-->
END%%
%%ANKI
Basic
What is the "conquer" step of merge sort?
Back: Call merge sort on the "divide"-step's two subarrays.
Reference: Thomas H. Cormen et al., *Introduction to Algorithms*, 3rd ed (Cambridge, Mass: MIT Press, 2009).
<!--ID: 1708742467121-->
END%%
%%ANKI
Basic
What is the "combine" step of merge sort?
Back: Merge the subarrays sorted after the "conquer" step.
Reference: Thomas H. Cormen et al., *Introduction to Algorithms*, 3rd ed (Cambridge, Mass: MIT Press, 2009).
<!--ID: 1708742467125-->
END%%
%%ANKI
Basic
When does merge sort "bottom out"?
Back: When the sequence to be sorted has length 1 or less.
Reference: Thomas H. Cormen et al., *Introduction to Algorithms*, 3rd ed (Cambridge, Mass: MIT Press, 2009).
<!--ID: 1708742467133-->
END%%
```c
static void merge(int i, int mid, int j, int *A) {
int si = mid - i + 1;
int sj = j - (mid + 1) + 1;
int *L = malloc(sizeof(int) * (si + 1));
int *R = malloc(sizeof(int) * (sj + 1));
L[si] = INT_MAX;
R[sj] = INT_MAX;
for (int k = 0; k < si; ++k) {
L[k] = A[i + k];
}
for (int k = 0; k < sj; ++k) {
R[k] = A[mid + 1 + k];
}
int topL = 0, topR = 0;
for (int k = 0; k < j - i + 1; ++k) {
if (L[topL] < R[topR]) {
A[i + k] = L[topL++];
} else {
A[i + k] = R[topR++];
}
}
free(L);
free(R);
}
void merge_sort(int i, int j, int *A) {
if (j <= i) {
return;
}
int mid = (i + j) / 2;
merge_sort(i, mid, A);
merge_sort(mid + 1, j, A);
merge(i, mid, j, A);
}
```
%%ANKI
Basic
Where in merge sort's implementation are sentinels useful?
Back: As the last elements of the two arrays to combine.
Reference: Thomas H. Cormen et al., *Introduction to Algorithms*, 3rd ed (Cambridge, Mass: MIT Press, 2009).
<!--ID: 1708742467137-->
END%%
%%ANKI
Basic
What sentinel values are typically used in merge sort's "merge" operation?
Back: $\infty$ or the record type's equivalent.
Reference: Thomas H. Cormen et al., *Introduction to Algorithms*, 3rd ed (Cambridge, Mass: MIT Press, 2009).
<!--ID: 1708742467141-->
END%%
%%ANKI
Basic
What sorting algorithm does the following demonstrate?
![[merge-sort.gif]]
Back: Merge sort.
Reference: Thomas H. Cormen et al., *Introduction to Algorithms*, 3rd ed (Cambridge, Mass: MIT Press, 2009).
<!--ID: 1708742590435-->
END%%
## References
* Thomas H. Cormen et al., *Introduction to Algorithms*, 3rd ed (Cambridge, Mass: MIT Press, 2009).