mirror of
https://github.com/krahets/hello-algo.git
synced 2026-08-15 13:10:59 +00:00
deploy
This commit is contained in:
@@ -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>
|
||||
@@ -1344,7 +1344,7 @@
|
||||
<span class="md-ellipsis">
|
||||
|
||||
|
||||
Chapter 4. Array and Linked List
|
||||
Chapter 4. Arrays and Linked Lists
|
||||
|
||||
|
||||
|
||||
@@ -1366,7 +1366,7 @@
|
||||
<span class="md-nav__icon md-icon"></span>
|
||||
|
||||
|
||||
Chapter 4. Array and Linked List
|
||||
Chapter 4. Arrays and Linked Lists
|
||||
|
||||
|
||||
</label>
|
||||
@@ -1472,7 +1472,7 @@
|
||||
<span class="md-ellipsis">
|
||||
|
||||
|
||||
4.4 Memory and Cache *
|
||||
4.4 Random-Access Memory and Cache *
|
||||
|
||||
|
||||
|
||||
@@ -1563,7 +1563,7 @@
|
||||
<span class="md-ellipsis">
|
||||
|
||||
|
||||
Chapter 5. Stack and Queue
|
||||
Chapter 5. Stacks and Queues
|
||||
|
||||
|
||||
|
||||
@@ -1585,7 +1585,7 @@
|
||||
<span class="md-nav__icon md-icon"></span>
|
||||
|
||||
|
||||
Chapter 5. Stack and Queue
|
||||
Chapter 5. Stacks and Queues
|
||||
|
||||
|
||||
</label>
|
||||
@@ -1663,7 +1663,7 @@
|
||||
<span class="md-ellipsis">
|
||||
|
||||
|
||||
5.3 Double-Ended Queue
|
||||
5.3 Deque
|
||||
|
||||
|
||||
|
||||
@@ -1754,7 +1754,7 @@
|
||||
<span class="md-ellipsis">
|
||||
|
||||
|
||||
Chapter 6. Hashing
|
||||
Chapter 6. Hash Table
|
||||
|
||||
|
||||
|
||||
@@ -1776,7 +1776,7 @@
|
||||
<span class="md-nav__icon md-icon"></span>
|
||||
|
||||
|
||||
Chapter 6. Hashing
|
||||
Chapter 6. Hash Table
|
||||
|
||||
|
||||
</label>
|
||||
@@ -2049,7 +2049,7 @@
|
||||
<span class="md-ellipsis">
|
||||
|
||||
|
||||
7.3 Array Representation of Tree
|
||||
7.3 Array Representation of Binary Trees
|
||||
|
||||
|
||||
|
||||
@@ -2268,7 +2268,7 @@
|
||||
<span class="md-ellipsis">
|
||||
|
||||
|
||||
8.2 Building a Heap
|
||||
8.2 Heap Construction Operation
|
||||
|
||||
|
||||
|
||||
@@ -2296,7 +2296,7 @@
|
||||
<span class="md-ellipsis">
|
||||
|
||||
|
||||
8.3 Top-K Problem
|
||||
8.3 Top-k Problem
|
||||
|
||||
|
||||
|
||||
@@ -2654,7 +2654,7 @@
|
||||
<span class="md-ellipsis">
|
||||
|
||||
|
||||
10.2 Binary Search Insertion
|
||||
10.2 Binary Search Insertion Point
|
||||
|
||||
|
||||
|
||||
@@ -2682,7 +2682,7 @@
|
||||
<span class="md-ellipsis">
|
||||
|
||||
|
||||
10.3 Binary Search Edge Cases
|
||||
10.3 Binary Search Boundaries
|
||||
|
||||
|
||||
|
||||
@@ -2738,7 +2738,7 @@
|
||||
<span class="md-ellipsis">
|
||||
|
||||
|
||||
10.5 Search Algorithms Revisited
|
||||
10.5 Searching Algorithms Revisited
|
||||
|
||||
|
||||
|
||||
@@ -2887,7 +2887,7 @@
|
||||
<span class="md-ellipsis">
|
||||
|
||||
|
||||
11.1 Sorting Algorithms
|
||||
11.1 Sorting Algorithm
|
||||
|
||||
|
||||
|
||||
@@ -3360,7 +3360,7 @@
|
||||
<span class="md-ellipsis">
|
||||
|
||||
|
||||
12.4 Hanoi Tower Problem
|
||||
12.4 Hanota Problem
|
||||
|
||||
|
||||
|
||||
@@ -4272,7 +4272,7 @@
|
||||
<span class="md-ellipsis">
|
||||
|
||||
|
||||
16.3 Terminology Table
|
||||
16.3 Glossary
|
||||
|
||||
|
||||
|
||||
@@ -4540,7 +4540,7 @@
|
||||
<p><u>Iteration</u> is a control structure for repeatedly executing a task. In iteration, a program repeatedly executes a segment of code under certain conditions until those conditions are no longer satisfied.</p>
|
||||
<h3 id="1-for-loop">1. For Loop<a class="headerlink" href="#1-for-loop" title="Permanent link">¶</a></h3>
|
||||
<p>The <code>for</code> loop is one of the most common forms of iteration, <strong>suitable for use when the number of iterations is known in advance</strong>.</p>
|
||||
<p>The following function implements the summation <span class="arithmatex">\(1 + 2 + \dots + n\)</span> based on a <code>for</code> loop, with the sum result recorded using the variable <code>res</code>. Note that in Python, <code>range(a, b)</code> corresponds to a "left-closed, right-open" interval, with the traversal range being <span class="arithmatex">\(a, a + 1, \dots, b-1\)</span>:</p>
|
||||
<p>The following function implements the summation <span class="arithmatex">\(1 + 2 + \dots + n\)</span> using a <code>for</code> loop, with the result stored in the variable <code>res</code>. Note that in Python, <code>range(a, b)</code> corresponds to a "left-closed, right-open" interval, with the traversal range being <span class="arithmatex">\(a, a + 1, \dots, b-1\)</span>:</p>
|
||||
<div class="tabbed-set tabbed-alternate" data-tabs="1:13"><input checked="checked" id="__tabbed_1_1" name="__tabbed_1" type="radio" /><input id="__tabbed_1_2" name="__tabbed_1" type="radio" /><input id="__tabbed_1_3" name="__tabbed_1" type="radio" /><input id="__tabbed_1_4" name="__tabbed_1" type="radio" /><input id="__tabbed_1_5" name="__tabbed_1" type="radio" /><input id="__tabbed_1_6" name="__tabbed_1" type="radio" /><input id="__tabbed_1_7" name="__tabbed_1" type="radio" /><input id="__tabbed_1_8" name="__tabbed_1" type="radio" /><input id="__tabbed_1_9" name="__tabbed_1" type="radio" /><input id="__tabbed_1_10" name="__tabbed_1" type="radio" /><input id="__tabbed_1_11" name="__tabbed_1" type="radio" /><input id="__tabbed_1_12" name="__tabbed_1" type="radio" /><input id="__tabbed_1_13" name="__tabbed_1" type="radio" /><div class="tabbed-labels"><label for="__tabbed_1_1">Python</label><label for="__tabbed_1_2">C++</label><label for="__tabbed_1_3">Java</label><label for="__tabbed_1_4">C#</label><label for="__tabbed_1_5">Go</label><label for="__tabbed_1_6">Swift</label><label for="__tabbed_1_7">JS</label><label for="__tabbed_1_8">TS</label><label for="__tabbed_1_9">Dart</label><label for="__tabbed_1_10">Rust</label><label for="__tabbed_1_11">C</label><label for="__tabbed_1_12">Kotlin</label><label for="__tabbed_1_13">Ruby</label></div>
|
||||
<div class="tabbed-content">
|
||||
<div class="tabbed-block">
|
||||
@@ -5324,7 +5324,7 @@
|
||||
<p align="center"> Figure 2-2 Flowchart of nested loops </p>
|
||||
|
||||
<p>In this case, the number of operations of the function is proportional to <span class="arithmatex">\(n^2\)</span>, or the algorithm's running time has a "quadratic relationship" with the input data size <span class="arithmatex">\(n\)</span>.</p>
|
||||
<p>We can continue adding nested loops, where each nesting is a "dimension increase", raising the time complexity to "cubic relationship", "quartic relationship", and so on.</p>
|
||||
<p>We can continue adding nested loops, where each additional level of nesting can be viewed as an increase in dimensionality, raising the time complexity to a "cubic relationship", a "quartic relationship", and so on.</p>
|
||||
<h2 id="222-recursion">2.2.2 Recursion<a class="headerlink" href="#222-recursion" title="Permanent link">¶</a></h2>
|
||||
<p><u>Recursion</u> is an algorithmic strategy that solves problems by having a function call itself. It mainly consists of two phases.</p>
|
||||
<ol>
|
||||
@@ -5524,7 +5524,7 @@
|
||||
<li><strong>Recursion</strong>: Decomposes the problem into the subproblem <span class="arithmatex">\(f(n) = n + f(n-1)\)</span>, continuously decomposing (recursively) until terminating at the base case <span class="arithmatex">\(f(1) = 1\)</span>.</li>
|
||||
</ul>
|
||||
<h3 id="1-call-stack">1. Call Stack<a class="headerlink" href="#1-call-stack" title="Permanent link">¶</a></h3>
|
||||
<p>Each time a recursive function calls itself, the system allocates memory for the newly opened function to store local variables, call addresses, and other information. This leads to two consequences.</p>
|
||||
<p>Each time a recursive function calls itself, the system allocates memory for the newly invoked function to store local variables, call addresses, and other information. This leads to two consequences.</p>
|
||||
<ul>
|
||||
<li>The function's context data is stored in a memory area called "stack frame space", which is not released until the function returns. Therefore, <strong>recursion usually consumes more memory space than iteration</strong>.</li>
|
||||
<li>Recursive function calls incur additional overhead. <strong>Therefore, recursion is usually less time-efficient than loops</strong>.</li>
|
||||
@@ -5535,7 +5535,7 @@
|
||||
|
||||
<p>In practice, the recursion depth allowed by programming languages is usually limited, and excessively deep recursion may lead to stack overflow errors.</p>
|
||||
<h3 id="2-tail-recursion">2. Tail Recursion<a class="headerlink" href="#2-tail-recursion" title="Permanent link">¶</a></h3>
|
||||
<p>Interestingly, <strong>if a function makes the recursive call as the very last step before returning</strong>, the function can be optimized by the compiler or interpreter to have space efficiency comparable to iteration. This case is called <u>tail recursion</u>.</p>
|
||||
<p>Interestingly, <strong>if a function makes the recursive call as the very last step before returning</strong>, the compiler or interpreter may optimize it so that its space efficiency is comparable to iteration. This case is called <u>tail recursion</u>.</p>
|
||||
<ul>
|
||||
<li><strong>Regular recursion</strong>: When a function returns to the previous level, it needs to continue executing code, so the system needs to save the context of the previous layer's call.</li>
|
||||
<li><strong>Tail recursion</strong>: The recursive call is the last operation before the function returns, meaning that after returning to the previous level, there is no need to continue executing other operations, so the system does not need to save the context of the previous layer's function.</li>
|
||||
@@ -5687,7 +5687,7 @@
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<p>The execution process of tail recursion is shown in Figure 2-5. Comparing regular recursion and tail recursion, the execution point of the summation operation is different.</p>
|
||||
<p>The execution process of tail recursion is shown in Figure 2-5. Comparing regular recursion and tail recursion, the summation operation is performed at different points.</p>
|
||||
<ul>
|
||||
<li><strong>Regular recursion</strong>: The summation operation is performed during the "ascending" process, requiring an additional summation operation after each layer returns.</li>
|
||||
<li><strong>Tail recursion</strong>: The summation operation is performed during the "descending" process; the "ascending" process only needs to return layer by layer.</li>
|
||||
@@ -5882,7 +5882,7 @@
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<p>Observing the above code, we recursively call two functions within the function, <strong>meaning that one call produces two call branches</strong>. As shown in Figure 2-6, such continuous recursive calling will eventually produce a <u>recursion tree</u> with <span class="arithmatex">\(n\)</span> levels.</p>
|
||||
<p>Observing the above code, we make two recursive calls within the function, <strong>meaning that one call produces two call branches</strong>. As shown in Figure 2-6, this repeated recursive calling eventually produces a <u>recursion tree</u> with <span class="arithmatex">\(n\)</span> levels.</p>
|
||||
<p><img alt="Recursion tree of the Fibonacci sequence" class="animation-figure" src="../iteration_and_recursion.assets/recursion_tree.png" /></p>
|
||||
<p align="center"> Figure 2-6 Recursion tree of the Fibonacci sequence </p>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user