Add more sorting algorithm details. Bubble sort.
parent
31c4a76589
commit
4982b6e21f
|
@ -74,12 +74,13 @@
|
|||
},
|
||||
"Added Media": [
|
||||
"insertion-sort.gif",
|
||||
"selection-sort.gif"
|
||||
"selection-sort.gif",
|
||||
"bubble-sort.gif"
|
||||
],
|
||||
"File Hashes": {
|
||||
"algorithms/index.md": "c9cf76a46508d3a1b3aeb54e7fb1f67d",
|
||||
"algorithms/sorting/index.md": "6fada1f3d5d3af64687719eb465a5b97",
|
||||
"algorithms/sorting/insertion-sort.md": "a531d611f4e2908fc4153b1b92077661",
|
||||
"algorithms/index.md": "1583c07edea4736db27c38fe2b6c4c31",
|
||||
"algorithms/sorting/index.md": "2d5a18a3079d96fa9e3d4289181a8b6c",
|
||||
"algorithms/sorting/insertion-sort.md": "e26909d0b4097e8e072d0b19667bf586",
|
||||
"bash/index.md": "3b5296277f095acdf16655adcdf524af",
|
||||
"bash/prompts.md": "61cb877e68da040a15b85af76b1f68ba",
|
||||
"bash/quoting.md": "b1d8869a91001f8b22f0cdc54d806f61",
|
||||
|
@ -118,16 +119,17 @@
|
|||
"algorithms/loop-invariants.md": "cbefc346842c21a6cce5c5edce451eb2",
|
||||
"algorithms/loop-invariant.md": "d883dfc997ee28a7a1e24b995377792b",
|
||||
"algorithms/running-time.md": "5efc0791097d2c996f931c9046c95f65",
|
||||
"algorithms/order-growth.md": "7f87c7ab55979d66fcda8776a35e3682",
|
||||
"algorithms/order-growth.md": "6e0f9e751eab24d74652851454264008",
|
||||
"_journal/2024-02-08.md": "19092bdfe378f31e2774f20d6afbfbac",
|
||||
"algorithms/sorting/selection-sort.md": "f31cf7e706504b9be48bed7af6e37074",
|
||||
"algorithms/sorting/selection-sort.md": "212140ad8bf70a56b73080bc7317f643",
|
||||
"algorithms/index 1.md": "6fada1f3d5d3af64687719eb465a5b97",
|
||||
"binary/hexadecimal.md": "a9633bbc9b53cc8c16ce6e56022f62e0",
|
||||
"binary/index.md": "d41d8cd98f00b204e9800998ecf8427e",
|
||||
"_journal/2024-02-09.md": "4a9ecfac710160c2d8d11077a739a97b",
|
||||
"c/types.md": "cf3e66e5aee58a94db3fdf0783908555",
|
||||
"logic/quantification.md": "b7cf646a8c33aa83f48ddc37c733fafb",
|
||||
"c/declarations.md": "381bb6ddbecd369b78012112b3a8e5de"
|
||||
"c/declarations.md": "381bb6ddbecd369b78012112b3a8e5de",
|
||||
"algorithms/sorting/bubble-sort.md": "6bffe3dabde3d6d4414ca3b5afdb1684"
|
||||
},
|
||||
"fields_dict": {
|
||||
"Basic": [
|
||||
|
|
|
@ -1,99 +1,3 @@
|
|||
---
|
||||
title: Sorting
|
||||
TARGET DECK: Obsidian::STEM
|
||||
FILE TAGS: algorithm::sorting
|
||||
tags:
|
||||
- algorithm
|
||||
- sorting
|
||||
title: Algorithms
|
||||
---
|
||||
|
||||
## Overview
|
||||
|
||||
Let $n \geq 0$. The **sorting problem** refers to permuting **records** $a_1, a_2, \ldots, a_n$ into a new sequence $\langle a_1', a_2', \ldots, a_n' \rangle$ such that $key(a_1') \leq key(a_2') \leq \cdots \leq key(a_n')$.
|
||||
|
||||
%%ANKI
|
||||
Cloze
|
||||
In the sorting problem, a "{record}" refers to {the entries being sorted}.
|
||||
Reference: Thomas H. Cormen et al., *Introduction to Algorithms*, 3rd ed (Cambridge, Mass: MIT Press, 2009).
|
||||
<!--ID: 1706981319280-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Cloze
|
||||
In the sorting problem, a "{key}" refers to {the value records are sorted by}.
|
||||
Reference: Thomas H. Cormen et al., *Introduction to Algorithms*, 3rd ed (Cambridge, Mass: MIT Press, 2009).
|
||||
<!--ID: 1706981319310-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Cloze
|
||||
In the sorting problem, "{satellite data}" refers to {the non-key values of records}.
|
||||
Reference: Thomas H. Cormen et al., *Introduction to Algorithms*, 3rd ed (Cambridge, Mass: MIT Press, 2009).
|
||||
<!--ID: 1706981319317-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
What term does Cormen et al. use to describe the record value used for sorting?
|
||||
Back: Keys.
|
||||
Reference: Thomas H. Cormen et al., *Introduction to Algorithms*, 3rd ed (Cambridge, Mass: MIT Press, 2009).
|
||||
<!--ID: 1706981319324-->
|
||||
END%%
|
||||
|
||||
%%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.
|
||||
|
||||
```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).
|
||||
<!--ID: 1706913303147-->
|
||||
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).
|
||||
<!--ID: 1706913303155-->
|
||||
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).
|
||||
<!--ID: 1706913303160-->
|
||||
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)
|
|
@ -9,7 +9,7 @@ tags:
|
|||
|
||||
## Overview
|
||||
|
||||
The **running time** of an algorithm is usually considered as a function of its **input size**. How input size is measured depends on the problem at hand. For instance, [[algorithms/index|sorting]] algorithms have an input size corresponding to the number of elements to sort.
|
||||
The **running time** of an algorithm is usually considered as a function of its **input size**. How input size is measured depends on the problem at hand. For instance, [[algorithms/sorting/index|sorting]] algorithms have an input size corresponding to the number of elements to sort.
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
|
|
Binary file not shown.
After Width: | Height: | Size: 9.9 KiB |
|
@ -0,0 +1,195 @@
|
|||
---
|
||||
title: Bubble Sort
|
||||
TARGET DECK: Obsidian::STEM
|
||||
FILE TAGS: algorithm::sorting
|
||||
tags:
|
||||
- algorithm
|
||||
- sorting
|
||||
---
|
||||
|
||||
## Overview
|
||||
|
||||
Property | Value
|
||||
----------- | --------
|
||||
Best Case | $\Omega(n^2)$
|
||||
Worst Case | $O(n^2)$
|
||||
Avg. Case | $O(n^2)$
|
||||
Aux. Memory | $O(1)$
|
||||
Stable | Yes
|
||||
Adaptive | Yes
|
||||
|
||||
![[bubble-sort.gif]]
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
What is bubble sort's best case runtime?
|
||||
Back: $\Omega(n)$
|
||||
Reference: Thomas H. Cormen et al., *Introduction to Algorithms*, 3rd ed (Cambridge, Mass: MIT Press, 2009).
|
||||
<!--ID: 1707504634781-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
How is it bubble sort achieves best case linear runtime?
|
||||
Back: By terminating when no swaps occurred on a given iteration.
|
||||
Reference: Thomas H. Cormen et al., *Introduction to Algorithms*, 3rd ed (Cambridge, Mass: MIT Press, 2009).
|
||||
<!--ID: 1707504634782-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
What input value does bubble 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: 1707504634784-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
What is bubble 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: 1707504634785-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
What input value does bubble 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: 1707504634787-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
What is bubble sort's average case runtime?
|
||||
Back: $O(n^2)$
|
||||
Reference: Thomas H. Cormen et al., *Introduction to Algorithms*, 3rd ed (Cambridge, Mass: MIT Press, 2009).
|
||||
<!--ID: 1707504634788-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
Is bubble sort in place?
|
||||
Back: Yes
|
||||
Reference: Thomas H. Cormen et al., *Introduction to Algorithms*, 3rd ed (Cambridge, Mass: MIT Press, 2009).
|
||||
<!--ID: 1707504634789-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
Is bubble sort stable?
|
||||
Back: Yes
|
||||
Reference: Thomas H. Cormen et al., *Introduction to Algorithms*, 3rd ed (Cambridge, Mass: MIT Press, 2009).
|
||||
<!--ID: 1707504634791-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
Is bubble sort adaptive?
|
||||
Back: Yes
|
||||
Reference: Thomas H. Cormen et al., *Introduction to Algorithms*, 3rd ed (Cambridge, Mass: MIT Press, 2009).
|
||||
<!--ID: 1707504634792-->
|
||||
END%%
|
||||
|
||||
```c
|
||||
void swap(int i, int j, int *A) {
|
||||
int tmp = A[i];
|
||||
A[i] = A[j];
|
||||
A[j] = tmp;
|
||||
}
|
||||
|
||||
void bubble_sort(const int n, int A[static n]) {
|
||||
bool swapped = true;
|
||||
for (int i = 0; swapped && i < n - 1; ++i) {
|
||||
swapped = false;
|
||||
for (int j = n - 1; j > i; --j) {
|
||||
if (A[j] < A[j - 1]) {
|
||||
swap(j, j - 1, A);
|
||||
swapped = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
What sorting algorithm does the following demonstrate?
|
||||
![[bubble-sort.gif]]
|
||||
Back: Bubble sort.
|
||||
Reference: Thomas H. Cormen et al., *Introduction to Algorithms*, 3rd ed (Cambridge, Mass: MIT Press, 2009).
|
||||
<!--ID: 1707504634794-->
|
||||
END%%
|
||||
|
||||
## Loop Invariant
|
||||
|
||||
Consider [[loop-invariant|loop invariant]] $P$ given by
|
||||
|
||||
> `A[0..i-1]` is a sorted array of the `i` least elements of `A`.
|
||||
|
||||
We prove $P$ maintains the requisite properties:
|
||||
|
||||
* Initialization
|
||||
* When `i = 0`, `A[0..-1]` is an empty array. This trivially satisfies $P$.
|
||||
* Maintenance
|
||||
* Suppose $P$ holds for some `0 ≤ i < n - 1`. Then `A[0..i-1]` is a sorted array of the `i` least elements of `A`. Our inner loop now starts at the end of the array and swaps each adjacent pair, putting the smaller of the two closer to position `i`. Repeating this process across all pairs from `n - 1` to `i + 1` ensures `A[i]` is the smallest element of `A[i..n-1]`. Therefore `A[0..i]` is a sorted array of the `i + 1` least elements of `A`. At the end of the iteration, `i` is incremented meaning `A[0..i-1]` still satisfies $P$.
|
||||
* Termination
|
||||
* Termination happens when `i = n - 1`. Then $P$ implies `A[0..n-2]` is a sorted array of the `n - 1` least elements of `A`. But then `A[n-1]` must be the greatest element of `A` meaning `A[0..n-1]`, the entire array, is in sorted order.
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
Given array `A[0..n-1]`, what is bubble sort's loop invariant?
|
||||
Back: `A[0..i-1]` is a sorted array of the `i` least elements of `A`.
|
||||
Reference: Thomas H. Cormen et al., *Introduction to Algorithms*, 3rd ed (Cambridge, Mass: MIT Press, 2009).
|
||||
<!--ID: 1707504634796-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
What is initialization of bubble sort's loop invariant?
|
||||
Back: Sorting starts with an empty array which is trivially sorted.
|
||||
Reference: Thomas H. Cormen et al., *Introduction to Algorithms*, 3rd ed (Cambridge, Mass: MIT Press, 2009).
|
||||
<!--ID: 1707504634797-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
What is maintenance of bubble sort's loop invariant?
|
||||
Back: Each iteration puts the next least element into the sorted subarray.
|
||||
Reference: Thomas H. Cormen et al., *Introduction to Algorithms*, 3rd ed (Cambridge, Mass: MIT Press, 2009).
|
||||
<!--ID: 1707504634798-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
How does bubble sort partition its input array?
|
||||
Back:
|
||||
```
|
||||
[ sorted | unsorted ]
|
||||
```
|
||||
Reference: Thomas H. Cormen et al., *Introduction to Algorithms*, 3rd ed (Cambridge, Mass: MIT Press, 2009).
|
||||
<!--ID: 1707504634800-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
Which element will bubble sort move to `sorted`?
|
||||
```
|
||||
[ sorted | unsorted ]
|
||||
```
|
||||
Back: The least element in `unsorted`.
|
||||
Reference: Thomas H. Cormen et al., *Introduction to Algorithms*, 3rd ed (Cambridge, Mass: MIT Press, 2009).
|
||||
<!--ID: 1707504634801-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Cloze
|
||||
Selection sort makes fewer {swaps} than bubble sort in the average case.
|
||||
Reference: Thomas H. Cormen et al., *Introduction to Algorithms*, 3rd ed (Cambridge, Mass: MIT Press, 2009).
|
||||
<!--ID: 1707504634803-->
|
||||
END%%
|
||||
|
||||
## References
|
||||
|
||||
* Thomas H. Cormen et al., *Introduction to Algorithms*, 3rd ed (Cambridge, Mass: MIT Press, 2009).
|
|
@ -0,0 +1,116 @@
|
|||
---
|
||||
title: Sorting
|
||||
TARGET DECK: Obsidian::STEM
|
||||
FILE TAGS: algorithm::sorting
|
||||
tags:
|
||||
- algorithm
|
||||
- sorting
|
||||
---
|
||||
|
||||
## Overview
|
||||
|
||||
Let $n \geq 0$. The **sorting problem** refers to permuting **records** $a_1, a_2, \ldots, a_n$ into a new sequence $\langle a_1', a_2', \ldots, a_n' \rangle$ such that $key(a_1') \leq key(a_2') \leq \cdots \leq key(a_n')$.
|
||||
|
||||
%%ANKI
|
||||
Cloze
|
||||
In the sorting problem, a "{record}" refers to {the entries being sorted}.
|
||||
Reference: Thomas H. Cormen et al., *Introduction to Algorithms*, 3rd ed (Cambridge, Mass: MIT Press, 2009).
|
||||
<!--ID: 1706981319280-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Cloze
|
||||
In the sorting problem, a "{key}" refers to {the value records are sorted by}.
|
||||
Reference: Thomas H. Cormen et al., *Introduction to Algorithms*, 3rd ed (Cambridge, Mass: MIT Press, 2009).
|
||||
<!--ID: 1706981319310-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Cloze
|
||||
In the sorting problem, "{satellite data}" refers to {the non-key values of records}.
|
||||
Reference: Thomas H. Cormen et al., *Introduction to Algorithms*, 3rd ed (Cambridge, Mass: MIT Press, 2009).
|
||||
<!--ID: 1706981319317-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
What term does Cormen et al. use to describe the record value used for sorting?
|
||||
Back: Keys.
|
||||
Reference: Thomas H. Cormen et al., *Introduction to Algorithms*, 3rd ed (Cambridge, Mass: MIT Press, 2009).
|
||||
<!--ID: 1706981319324-->
|
||||
END%%
|
||||
|
||||
A few key terms are used to describe properties of sorting algorithms:
|
||||
|
||||
* Stability
|
||||
* An algorithm is **stable** if values that compare as equal are ordered the same in the output as they are in the input.
|
||||
* In Place
|
||||
* An algorithm is **in place** if only a constant number of input values are ever stored outside the array.
|
||||
* Adaptive
|
||||
* An algorithm is **adaptive** if it takes advantage of existing order in its input.
|
||||
|
||||
%%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%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
What is an adaptive sorting algorithm?
|
||||
Back: One that takes advantage of existing order in its input.
|
||||
Reference: Thomas H. Cormen et al., *Introduction to Algorithms*, 3rd ed (Cambridge, Mass: MIT Press, 2009).
|
||||
<!--ID: 1707504634774-->
|
||||
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.
|
||||
|
||||
```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).
|
||||
<!--ID: 1706913303147-->
|
||||
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).
|
||||
<!--ID: 1706913303155-->
|
||||
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).
|
||||
<!--ID: 1706913303160-->
|
||||
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)
|
|
@ -9,21 +9,21 @@ tags:
|
|||
|
||||
## Overview
|
||||
|
||||
Property | Value
|
||||
---------- | --------
|
||||
Best Case | $O(n)$
|
||||
Worst Case | $O(n^2)$
|
||||
Avg. Case | $O(n^2)$
|
||||
Memory | $O(1)$
|
||||
In place | Yes
|
||||
Stable | Yes
|
||||
Property | Value
|
||||
----------- | --------
|
||||
Best Case | $\Omega(n)$
|
||||
Worst Case | $O(n^2)$
|
||||
Avg. Case | $O(n^2)$
|
||||
Aux. Memory | $O(1)$
|
||||
Stable | Yes
|
||||
Adaptive | Yes
|
||||
|
||||
![[insertion-sort.gif]]
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
What is insertion sort's best case runtime?
|
||||
Back: $O(n)$
|
||||
Back: $\Omega(n)$
|
||||
Reference: Thomas H. Cormen et al., *Introduction to Algorithms*, 3rd ed (Cambridge, Mass: MIT Press, 2009).
|
||||
<!--ID: 1706925879541-->
|
||||
END%%
|
||||
|
@ -76,6 +76,14 @@ Reference: Thomas H. Cormen et al., *Introduction to Algorithms*, 3rd ed (Cambri
|
|||
<!--ID: 1706926586959-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
Is insertion sort adaptive?
|
||||
Back: Yes
|
||||
Reference: Thomas H. Cormen et al., *Introduction to Algorithms*, 3rd ed (Cambridge, Mass: MIT Press, 2009).
|
||||
<!--ID: 1707504634779-->
|
||||
END%%
|
||||
|
||||
```c
|
||||
void insertion_sort(const int n, int A[static n]) {
|
||||
for (int i = 1; i < n; ++i) {
|
||||
|
@ -136,6 +144,13 @@ Reference: Thomas H. Cormen et al., *Introduction to Algorithms*, 3rd ed (Cambri
|
|||
<!--ID: 1707332638375-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
insertion sort makes fewer {comparisons} than selection sort in the average case.
|
||||
Reference: Thomas H. Cormen et al., *Introduction to Algorithms*, 3rd ed (Cambridge, Mass: MIT Press, 2009).
|
||||
<!--ID: 1707500283783-->
|
||||
END%%
|
||||
|
||||
## 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.
|
||||
|
|
|
@ -9,21 +9,21 @@ tags:
|
|||
|
||||
## Overview
|
||||
|
||||
Property | Value
|
||||
---------- | --------
|
||||
Best Case | $O(n^2)$
|
||||
Worst Case | $O(n^2)$
|
||||
Avg. Case | $O(n^2)$
|
||||
Memory | $O(1)$
|
||||
In Place | Yes
|
||||
Stable | Yes
|
||||
Property | Value
|
||||
----------- | --------
|
||||
Best Case | $\Omega(n^2)$
|
||||
Worst Case | $O(n^2)$
|
||||
Avg. Case | $O(n^2)$
|
||||
Aux. Memory | $O(1)$
|
||||
Stable | No
|
||||
Adaptive | No
|
||||
|
||||
![[selection-sort.gif]]
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
What is selection sort's best case runtime?
|
||||
Back: $O(n^2)$
|
||||
Back: $\Omega(n^2)$
|
||||
Reference: Thomas H. Cormen et al., *Introduction to Algorithms*, 3rd ed (Cambridge, Mass: MIT Press, 2009).
|
||||
<!--ID: 1707398773323-->
|
||||
END%%
|
||||
|
@ -55,11 +55,19 @@ END%%
|
|||
%%ANKI
|
||||
Basic
|
||||
Is selection sort stable?
|
||||
Back: Yes
|
||||
Back: No
|
||||
Reference: Thomas H. Cormen et al., *Introduction to Algorithms*, 3rd ed (Cambridge, Mass: MIT Press, 2009).
|
||||
<!--ID: 1707398773330-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
Is selection sort adaptive?
|
||||
Back: No
|
||||
Reference: Thomas H. Cormen et al., *Introduction to Algorithms*, 3rd ed (Cambridge, Mass: MIT Press, 2009).
|
||||
<!--ID: 1707504634778-->
|
||||
END%%
|
||||
|
||||
```c
|
||||
void swap(int i, int j, int *A) {
|
||||
int tmp = A[i];
|
||||
|
@ -93,14 +101,14 @@ END%%
|
|||
|
||||
Consider [[loop-invariant|loop invariant]] $P$ given by
|
||||
|
||||
> On each iteration, `A[0..i-1]` is a sorted array of the `i` least elements of `A`.
|
||||
> `A[0..i-1]` is a sorted array of the `i` least elements of `A`.
|
||||
|
||||
We prove $P$ maintains the requisite properties:
|
||||
|
||||
* Initialization
|
||||
* When `i = 0`, `A[0..-1]` is an empty array. This trivially satisfies $P$.
|
||||
* Maintenance
|
||||
* Suppose $P$ holds for some `0 ≤ i < n - 1`. Then `A[0..i-1]` is a sorted array of the `i` least elements of `A`. Our inner loop then finds the smallest element in `A[i..n]` and swaps it with `A[i]`. Therefore `A[0..i]` is not a sorted array of the `i + 1` least elements of `A`. At the end of the iteration, `i` is incremented meaning `A[0..i-1]` still satisfies $P$.
|
||||
* Suppose $P$ holds for some `0 ≤ i < n - 1`. Then `A[0..i-1]` is a sorted array of the `i` least elements of `A`. Our inner loop then finds the smallest element in `A[i..n]` and swaps it with `A[i]`. Therefore `A[0..i]` is a sorted array of the `i + 1` least elements of `A`. At the end of the iteration, `i` is incremented meaning `A[0..i-1]` still satisfies $P$.
|
||||
* Termination
|
||||
* On termination, `i = n - 1` and `A[0..n-2]` are the `n - 1` least elements of `A` in sorted order. But, by exhaustion, `A[n-1]` must be the largest element meaning `A[0..n-1]`, the entire array, is in sorted order.
|
||||
|
||||
|
@ -150,6 +158,13 @@ Reference: Thomas H. Cormen et al., *Introduction to Algorithms*, 3rd ed (Cambri
|
|||
<!--ID: 1707399790955-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Cloze
|
||||
Selection sort makes fewer {swaps} than insertion sort in the average case.
|
||||
Reference: Thomas H. Cormen et al., *Introduction to Algorithms*, 3rd ed (Cambridge, Mass: MIT Press, 2009).
|
||||
<!--ID: 1707500283779-->
|
||||
END%%
|
||||
|
||||
## References
|
||||
|
||||
* Thomas H. Cormen et al., *Introduction to Algorithms*, 3rd ed (Cambridge, Mass: MIT Press, 2009).
|
||||
|
|
Loading…
Reference in New Issue