This commit is contained in:
krahets
2026-04-02 03:08:50 +08:00
parent 09a136c9fa
commit aaf9f58eb3
157 changed files with 3002 additions and 2994 deletions
@@ -6,7 +6,7 @@
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta name="description" content="Data Structures and Algorithms Crash Course with Animated Illustrations and Off-the-Shelf Code">
<meta name="description" content="Data structures and algorithms tutorial with animated illustrations and ready-to-run code">
<meta name="author" content="krahets">
@@ -576,7 +576,7 @@
<span class="md-ellipsis">
Chapter 1. Encounter With Algorithms
Chapter 1. Encounter with Algorithms
@@ -598,7 +598,7 @@
<span class="md-nav__icon md-icon"></span>
Chapter 1. Encounter With Algorithms
Chapter 1. Encounter with Algorithms
</label>
@@ -1183,7 +1183,7 @@
<span class="md-ellipsis">
Chapter 4. Array and Linked List
Chapter 4. Arrays and Linked Lists
@@ -1205,7 +1205,7 @@
<span class="md-nav__icon md-icon"></span>
Chapter 4. Array and Linked List
Chapter 4. Arrays and Linked Lists
</label>
@@ -1311,7 +1311,7 @@
<span class="md-ellipsis">
4.4 Memory and Cache *
4.4 Random-Access Memory and Cache *
@@ -1402,7 +1402,7 @@
<span class="md-ellipsis">
Chapter 5. Stack and Queue
Chapter 5. Stacks and Queues
@@ -1424,7 +1424,7 @@
<span class="md-nav__icon md-icon"></span>
Chapter 5. Stack and Queue
Chapter 5. Stacks and Queues
</label>
@@ -1502,7 +1502,7 @@
<span class="md-ellipsis">
5.3 Double-Ended Queue
5.3 Deque
@@ -1593,7 +1593,7 @@
<span class="md-ellipsis">
Chapter 6. Hashing
Chapter 6. Hash Table
@@ -1615,7 +1615,7 @@
<span class="md-nav__icon md-icon"></span>
Chapter 6. Hashing
Chapter 6. Hash Table
</label>
@@ -1888,7 +1888,7 @@
<span class="md-ellipsis">
7.3 Array Representation of Tree
7.3 Array Representation of Binary Trees
@@ -2107,7 +2107,7 @@
<span class="md-ellipsis">
8.2 Building a Heap
8.2 Heap Construction Operation
@@ -2135,7 +2135,7 @@
<span class="md-ellipsis">
8.3 Top-K Problem
8.3 Top-k Problem
@@ -2493,7 +2493,7 @@
<span class="md-ellipsis">
10.2 Binary Search Insertion
10.2 Binary Search Insertion Point
@@ -2521,7 +2521,7 @@
<span class="md-ellipsis">
10.3 Binary Search Edge Cases
10.3 Binary Search Boundaries
@@ -2577,7 +2577,7 @@
<span class="md-ellipsis">
10.5 Search Algorithms Revisited
10.5 Searching Algorithms Revisited
@@ -2726,7 +2726,7 @@
<span class="md-ellipsis">
11.1 Sorting Algorithms
11.1 Sorting Algorithm
@@ -3260,7 +3260,7 @@
<span class="md-ellipsis">
12.4 Hanoi Tower Problem
12.4 Hanota Problem
@@ -4172,7 +4172,7 @@
<span class="md-ellipsis">
16.3 Terminology Table
16.3 Glossary
@@ -4338,7 +4338,7 @@
<p>We have already learned that search algorithms are divided into two major categories.</p>
<ul>
<li><strong>Brute-force search</strong>: Implemented by traversing the data structure, with a time complexity of <span class="arithmatex">\(O(n)\)</span>.</li>
<li><strong>Adaptive search</strong>: Utilizes unique data organization forms or prior information, with time complexity reaching <span class="arithmatex">\(O(\log n)\)</span> or even <span class="arithmatex">\(O(1)\)</span>.</li>
<li><strong>Adaptive search</strong>: Leverages specific data organization or prior information, with time complexity reaching <span class="arithmatex">\(O(\log n)\)</span> or even <span class="arithmatex">\(O(1)\)</span>.</li>
</ul>
<p>In fact, <strong>search algorithms with time complexity of <span class="arithmatex">\(O(\log n)\)</span> are typically implemented based on the divide and conquer strategy</strong>, such as binary search and trees.</p>
<ul>
@@ -4356,14 +4356,14 @@
<p>In previous sections, binary search was implemented based on iteration. Now we implement it based on divide and conquer (recursion).</p>
<div class="admonition question">
<p class="admonition-title">Question</p>
<p>Given a sorted array <code>nums</code> of length <span class="arithmatex">\(n\)</span>, where all elements are unique, find the element <code>target</code>.</p>
<p>Given a sorted array <code>nums</code> of length <span class="arithmatex">\(n\)</span>, where all elements are unique, find <code>target</code>.</p>
</div>
<p>From a divide and conquer perspective, we denote the subproblem corresponding to the search interval <span class="arithmatex">\([i, j]\)</span> as <span class="arithmatex">\(f(i, j)\)</span>.</p>
<p>Starting from the original problem <span class="arithmatex">\(f(0, n-1)\)</span>, perform binary search through the following steps.</p>
<ol>
<li>Calculate the midpoint <span class="arithmatex">\(m\)</span> of the search interval <span class="arithmatex">\([i, j]\)</span>, and use it to eliminate half of the search interval.</li>
<li>Recursively solve the subproblem reduced by half in size, which could be <span class="arithmatex">\(f(i, m-1)\)</span> or <span class="arithmatex">\(f(m+1, j)\)</span>.</li>
<li>Repeat steps <code>1.</code> and <code>2.</code> until <code>target</code> is found or the interval is empty and return.</li>
<li>Repeat steps <code>1.</code> and <code>2.</code> until <code>target</code> is found, or return when the interval is empty.</li>
</ol>
<p>Figure 12-4 shows the divide and conquer process of binary search for element <span class="arithmatex">\(6\)</span> in an array.</p>
<p><img alt="Divide and conquer process of binary search" class="animation-figure" src="../binary_search_recur.assets/binary_search_recur.png" /></p>