Skip to content

Commit e705b2a

Browse files
committed
Built site for gh-pages
1 parent 267a733 commit e705b2a

7 files changed

Lines changed: 25 additions & 25 deletions

File tree

.nojekyll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
b76563c3
1+
89fe6881

01_search.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@
172172
<li class="sidebar-item">
173173
<div class="sidebar-item-container">
174174
<a href="./03_sort.html" class="sidebar-item-text sidebar-link">
175-
<span class="menu-text"><span class="chapter-number">3</span>&nbsp; <span class="chapter-title">basic Sorting</span></span></a>
175+
<span class="menu-text"><span class="chapter-number">3</span>&nbsp; <span class="chapter-title">Basic Sorting</span></span></a>
176176
</div>
177177
</li>
178178
</ul>

02_binary.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@
201201
<li class="sidebar-item">
202202
<div class="sidebar-item-container">
203203
<a href="./03_sort.html" class="sidebar-item-text sidebar-link">
204-
<span class="menu-text"><span class="chapter-number">3</span>&nbsp; <span class="chapter-title">basic Sorting</span></span></a>
204+
<span class="menu-text"><span class="chapter-number">3</span>&nbsp; <span class="chapter-title">Basic Sorting</span></span></a>
205205
</div>
206206
</li>
207207
</ul>
@@ -799,8 +799,8 @@ <h2 data-number="2.3" class="anchored" data-anchor-id="conclusion"><span class="
799799
</a>
800800
</div>
801801
<div class="nav-page nav-page-next">
802-
<a href="./03_sort.html" class="pagination-link" aria-label="basic Sorting">
803-
<span class="nav-page-text"><span class="chapter-number">3</span>&nbsp; <span class="chapter-title">basic Sorting</span></span> <i class="bi bi-arrow-right-short"></i>
802+
<a href="./03_sort.html" class="pagination-link" aria-label="Basic Sorting">
803+
<span class="nav-page-text"><span class="chapter-number">3</span>&nbsp; <span class="chapter-title">Basic Sorting</span></span> <i class="bi bi-arrow-right-short"></i>
804804
</a>
805805
</div>
806806
</nav>

03_sort.html

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
88

99

10-
<title>3&nbsp; basic Sorting – The Algorithm Codex</title>
10+
<title>3&nbsp; Basic Sorting – The Algorithm Codex</title>
1111
<style>
1212
code{white-space: pre-wrap;}
1313
span.smallcaps{font-variant: small-caps;}
@@ -145,7 +145,7 @@
145145
<button type="button" class="quarto-btn-toggle btn" data-bs-toggle="collapse" role="button" data-bs-target=".quarto-sidebar-collapse-item" aria-controls="quarto-sidebar" aria-expanded="false" aria-label="Toggle sidebar navigation" onclick="if (window.quartoToggleHeadroom) { window.quartoToggleHeadroom(); }">
146146
<i class="bi bi-layout-text-sidebar-reverse"></i>
147147
</button>
148-
<nav class="quarto-page-breadcrumbs" aria-label="breadcrumb"><ol class="breadcrumb"><li class="breadcrumb-item"><a href="./01_search.html">Searching and Sorting</a></li><li class="breadcrumb-item"><a href="./03_sort.html"><span class="chapter-number">3</span>&nbsp; <span class="chapter-title">basic Sorting</span></a></li></ol></nav>
148+
<nav class="quarto-page-breadcrumbs" aria-label="breadcrumb"><ol class="breadcrumb"><li class="breadcrumb-item"><a href="./01_search.html">Searching and Sorting</a></li><li class="breadcrumb-item"><a href="./03_sort.html"><span class="chapter-number">3</span>&nbsp; <span class="chapter-title">Basic Sorting</span></a></li></ol></nav>
149149
<a class="flex-grow-1" role="navigation" data-bs-toggle="collapse" data-bs-target=".quarto-sidebar-collapse-item" aria-controls="quarto-sidebar" aria-expanded="false" aria-label="Toggle sidebar navigation" onclick="if (window.quartoToggleHeadroom) { window.quartoToggleHeadroom(); }">
150150
</a>
151151
<button type="button" class="btn quarto-search-button" aria-label="Search" onclick="window.quartoOpenSearch();">
@@ -200,7 +200,7 @@
200200
<li class="sidebar-item">
201201
<div class="sidebar-item-container">
202202
<a href="./03_sort.html" class="sidebar-item-text sidebar-link active">
203-
<span class="menu-text"><span class="chapter-number">3</span>&nbsp; <span class="chapter-title">basic Sorting</span></span></a>
203+
<span class="menu-text"><span class="chapter-number">3</span>&nbsp; <span class="chapter-title">Basic Sorting</span></span></a>
204204
</div>
205205
</li>
206206
</ul>
@@ -255,9 +255,9 @@ <h2 id="toc-title">Table of contents</h2>
255255
<!-- main -->
256256
<main class="content" id="quarto-document-content">
257257

258-
<header id="title-block-header" class="quarto-title-block default"><nav class="quarto-page-breadcrumbs quarto-title-breadcrumbs d-none d-lg-block" aria-label="breadcrumb"><ol class="breadcrumb"><li class="breadcrumb-item"><a href="./01_search.html">Searching and Sorting</a></li><li class="breadcrumb-item"><a href="./03_sort.html"><span class="chapter-number">3</span>&nbsp; <span class="chapter-title">basic Sorting</span></a></li></ol></nav>
258+
<header id="title-block-header" class="quarto-title-block default"><nav class="quarto-page-breadcrumbs quarto-title-breadcrumbs d-none d-lg-block" aria-label="breadcrumb"><ol class="breadcrumb"><li class="breadcrumb-item"><a href="./01_search.html">Searching and Sorting</a></li><li class="breadcrumb-item"><a href="./03_sort.html"><span class="chapter-number">3</span>&nbsp; <span class="chapter-title">Basic Sorting</span></a></li></ol></nav>
259259
<div class="quarto-title">
260-
<h1 class="title"><span class="chapter-number">3</span>&nbsp; <span class="chapter-title">basic Sorting</span></h1>
260+
<h1 class="title"><span class="chapter-number">3</span>&nbsp; <span class="chapter-title">Basic Sorting</span></h1>
261261
</div>
262262

263263

The-Algorithm-Codex.pdf

-1 Bytes
Binary file not shown.

index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@
138138
<li class="sidebar-item">
139139
<div class="sidebar-item-container">
140140
<a href="./03_sort.html" class="sidebar-item-text sidebar-link">
141-
<span class="menu-text"><span class="chapter-number">3</span>&nbsp; <span class="chapter-title">basic Sorting</span></span></a>
141+
<span class="menu-text"><span class="chapter-number">3</span>&nbsp; <span class="chapter-title">Basic Sorting</span></span></a>
142142
</div>
143143
</li>
144144
</ul>

search.json

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -151,78 +151,78 @@
151151
{
152152
"objectID": "03_sort.html",
153153
"href": "03_sort.html",
154-
"title": "basic Sorting",
154+
"title": "Basic Sorting",
155155
"section": "",
156156
"text": "3.1 Selection Sort\nAs we discovered in the previous chapter, structure is the secret ingredient that makes computation efficient. Searching through an unordered collection is a tedious, linear process, but searching through an ordered one is exponentially faster.\nSorting is the process of establishing this order. Formally, we want to take a sequence of items and rearrange them into a new sequence where, for any two indices \\(i\\) and \\(j\\), if \\(i &lt; j\\), then \\(x_i &lt; x_j\\). In this chapter, we explore the most fundamental ways to achieve this, building our intuition from simple observation to a deeper structural analysis of the sorting problem itself.\nThe most intuitive way to sort a list is to think about the destination. If we are building a sorted sequence, the very first element (index 0) must be the minimum element of the entire collection. Once that is in place, the second element (index 1) must be the minimum of everything that remains, and so on.\nIn Selection Sort, we stand at each position in the array and ask: “Which element belongs here?” To answer, we scan the unsorted portion of the list, find the minimum, and swap it into our current position.\nWe implement this by directly searching for the minimum index in each iteration rather than calling a helper function, keeping the logic self-contained. Because we must scan the remaining items for every single position in the list, we perform roughly \\(1 + 2 + 3 + ... (n-1) = n(n-1)/2\\) comparisons, leading to a time complexity of \\(O(n^2)\\).",
157157
"crumbs": [
158158
"Searching and Sorting",
159-
"<span class='chapter-number'>3</span>  <span class='chapter-title'>basic Sorting</span>"
159+
"<span class='chapter-number'>3</span>  <span class='chapter-title'>Basic Sorting</span>"
160160
]
161161
},
162162
{
163163
"objectID": "03_sort.html#selection-sort",
164164
"href": "03_sort.html#selection-sort",
165-
"title": "basic Sorting",
165+
"title": "Basic Sorting",
166166
"section": "",
167167
"text": "from typing import MutableSequence\nfrom codex.types import Ordering, default_order\n\ndef selection_sort[T](\n items: MutableSequence[T], f: Ordering[T] = None\n) -&gt; None:\n if f is None:\n f = default_order\n\n n = len(items)\n for i in range(n):\n # Find the index of the minimum element in the unsorted suffix\n min_idx = i\n for j in range(i + 1, n):\n if f(items[j], items[min_idx]) &lt; 0:\n min_idx = j\n\n # Swap it into the current position\n items[i], items[min_idx] = items[min_idx], items[i]",
168168
"crumbs": [
169169
"Searching and Sorting",
170-
"<span class='chapter-number'>3</span>  <span class='chapter-title'>basic Sorting</span>"
170+
"<span class='chapter-number'>3</span>  <span class='chapter-title'>Basic Sorting</span>"
171171
]
172172
},
173173
{
174174
"objectID": "03_sort.html#insertion-sort",
175175
"href": "03_sort.html#insertion-sort",
176-
"title": "basic Sorting",
176+
"title": "Basic Sorting",
177177
"section": "3.2 Insertion Sort",
178178
"text": "3.2 Insertion Sort\nWe can flip the narrative of Selection Sort. Instead of standing at a position and looking for the right element, we can take an element and look for its right position. This is how most people sort a hand of cards.\nWe assume that everything behind our current position is already sorted. We take the next element and ask: “How far back must I move this element so that the sequence remains sorted?” We shift it backward, swapping it with its predecessor, until it finds its rightful place.\ndef insertion_sort[T](\n items: MutableSequence[T], f: Ordering[T] = None\n) -&gt; None:\n if f is None:\n f = default_order\n\n for i in range(1, len(items)):\n j = i\n # Move the element backward as long as it is smaller than its predecessor\n while j &gt; 0 and f(items[j], items[j-1]) &lt; 0:\n items[j], items[j-1] = items[j-1], items[j]\n j -= 1\nThe first element is sorted by definition. The second element either stays put or moves before the first. The third moves until it is in the correct spot relative to the first two. In the worst case (a reverse-sorted list), this also results again in \\(O(n^2)\\) operations, but it is remarkably efficient for lists that are already “nearly sorted.”",
179179
"crumbs": [
180180
"Searching and Sorting",
181-
"<span class='chapter-number'>3</span>  <span class='chapter-title'>basic Sorting</span>"
181+
"<span class='chapter-number'>3</span>  <span class='chapter-title'>Basic Sorting</span>"
182182
]
183183
},
184184
{
185185
"objectID": "03_sort.html#the-geometry-of-inversions",
186186
"href": "03_sort.html#the-geometry-of-inversions",
187-
"title": "basic Sorting",
187+
"title": "Basic Sorting",
188188
"section": "3.3 The Geometry of Inversions",
189189
"text": "3.3 The Geometry of Inversions\nTo understand why these algorithms are all quadratic in complexity, we need to look at the structure of “unsortedness.” Whenever a list in unsorted, is because we can find at least a couple of elements that are out of place. This means some \\(x_i &gt; x_j\\) where \\(i &lt; j\\). We define an inversion as any pair of such items. A sorted list has zero inversions.\nWith this idea in place, we can see sorting as “just” the problem of reducing the number of inversions down to zero. Any algorithm that does progress towards reducing the number of inversions is actually sorting. And a crucial insight is that there can be at most \\(O(n^2)\\) inversions in any sequence of size \\(n\\).\nSelection sort reduces up to \\(n\\) inversions with each swap, that is, all inversions relative to the current minimum element. But every swap requires up to \\(n\\) comparisons, so we get \\(O(n^2)\\) steps. Insertion sort reduces at most one inversion each step, by moving one item forward, thus it will require \\(O(n^2)\\) steps to eliminate that many inversions.",
190190
"crumbs": [
191191
"Searching and Sorting",
192-
"<span class='chapter-number'>3</span>  <span class='chapter-title'>basic Sorting</span>"
192+
"<span class='chapter-number'>3</span>  <span class='chapter-title'>Basic Sorting</span>"
193193
]
194194
},
195195
{
196196
"objectID": "03_sort.html#bubble-sort",
197197
"href": "03_sort.html#bubble-sort",
198-
"title": "basic Sorting",
198+
"title": "Basic Sorting",
199199
"section": "3.4 Bubble Sort",
200200
"text": "3.4 Bubble Sort\nLet’s now build an algorithm based on this idea of eliminating inversions directly. A first guess could be, lets try to find a pair of inverted items and swap them. But we must be careful, if we do indiscrimantely, we might end up fixing one inversion but creating other inversions.\nHowever, a powerful idea that we won’t formally proof is that if a list has any inversions, there must be at least one inversion between two consecutive elements. If we fix these local inversions, we eventually fix them all. And fixing an inversion between consecutive elements cannot create new inversions. This is the heart of Bubble Sort: we repeatedly step through the list and swap adjacent items that are out of order.\ndef bubble_sort[T](\n items: MutableSequence[T], f: Ordering[T] = None\n) -&gt; None:\n if f is None:\n f = default_order\n\n n = len(items)\n for i in range(n):\n swapped = False\n for j in range(0, n - i - 1):\n # If we find a consecutive inversion, fix it\n if f(items[j+1], items[j]) &lt; 0:\n items[j], items[j+1] = items[j+1], items[j]\n swapped = True\n\n # If no swaps occurred, the list is already sorted\n if not swapped:\n break",
201201
"crumbs": [
202202
"Searching and Sorting",
203-
"<span class='chapter-number'>3</span>  <span class='chapter-title'>basic Sorting</span>"
203+
"<span class='chapter-number'>3</span>  <span class='chapter-title'>Basic Sorting</span>"
204204
]
205205
},
206206
{
207207
"objectID": "03_sort.html#verification",
208208
"href": "03_sort.html#verification",
209-
"title": "basic Sorting",
209+
"title": "Basic Sorting",
210210
"section": "3.5 Verification",
211211
"text": "3.5 Verification\nTo ensure these three fundamental sorting approaches work as intended, we can run them against a set of standard cases.\nimport pytest\nfrom codex.sort.basic import selection_sort, insertion_sort, bubble_sort\n\n@pytest.mark.parametrize(\"sort_fn\", [selection_sort, insertion_sort, bubble_sort])\ndef test_sorting_algorithms(sort_fn):\n items = [4, 2, 7, 1, 3]\n sort_fn(items)\n assert items == [1, 2, 3, 4, 7]\n\n # Already sorted\n items = [1, 2, 3]\n sort_fn(items)\n assert items == [1, 2, 3]\n\n # Reverse sorted\n items = [3, 2, 1]\n sort_fn(items)\n assert items == [1, 2, 3]",
212212
"crumbs": [
213213
"Searching and Sorting",
214-
"<span class='chapter-number'>3</span>  <span class='chapter-title'>basic Sorting</span>"
214+
"<span class='chapter-number'>3</span>  <span class='chapter-title'>Basic Sorting</span>"
215215
]
216216
},
217217
{
218218
"objectID": "03_sort.html#conclusion",
219219
"href": "03_sort.html#conclusion",
220-
"title": "basic Sorting",
220+
"title": "Basic Sorting",
221221
"section": "3.6 Conclusion",
222222
"text": "3.6 Conclusion\nSelection, Insertion, and Bubble sort are all \\(O(n^2)\\) algorithms. The reason is structural: in the worst case, a list of size can have \\(O(n^2)\\) inversions. Since each swap in these algorithms only fixes one inversion at a time–in the best case–we are forced to perform a quadratic number of operations.\nTo break this ceiling and reach the theoretical limit of \\(O(n^2)\\), we need to be more clever. We need algorithms that can fix many inversions with a single operation. This “divide and conquer” approach will be the focus of our next chapter: Efficient Sorting.",
223223
"crumbs": [
224224
"Searching and Sorting",
225-
"<span class='chapter-number'>3</span>  <span class='chapter-title'>basic Sorting</span>"
225+
"<span class='chapter-number'>3</span>  <span class='chapter-title'>Basic Sorting</span>"
226226
]
227227
}
228228
]

0 commit comments

Comments
 (0)