mirror of
https://github.com/krahets/hello-algo.git
synced 2025-07-10 08:50:20 +08:00
deploy
This commit is contained in:
@ -26,7 +26,7 @@
|
||||
|
||||
|
||||
|
||||
<title>2.2 Iteration and Recursion - Hello Algo</title>
|
||||
<title>2.2 Iteration and recursion - Hello Algo</title>
|
||||
|
||||
|
||||
|
||||
@ -153,7 +153,7 @@
|
||||
<div class="md-header__topic" data-md-component="header-topic">
|
||||
<span class="md-ellipsis">
|
||||
|
||||
2.2 Iteration and Recursion
|
||||
2.2 Iteration and recursion
|
||||
|
||||
</span>
|
||||
</div>
|
||||
@ -201,7 +201,13 @@
|
||||
|
||||
<li class="md-select__item">
|
||||
<a href="/" hreflang="zh" class="md-select__link">
|
||||
中文
|
||||
简体中文
|
||||
</a>
|
||||
</li>
|
||||
|
||||
<li class="md-select__item">
|
||||
<a href="/zh-hant/" hreflang="zh-Hant" class="md-select__link">
|
||||
繁體中文
|
||||
</a>
|
||||
</li>
|
||||
|
||||
@ -393,7 +399,7 @@
|
||||
|
||||
|
||||
<span class="md-ellipsis">
|
||||
0.1 About This Book
|
||||
0.1 About this book
|
||||
</span>
|
||||
|
||||
|
||||
@ -414,7 +420,7 @@
|
||||
|
||||
|
||||
<span class="md-ellipsis">
|
||||
0.2 How to Read
|
||||
0.2 How to read
|
||||
</span>
|
||||
|
||||
|
||||
@ -491,7 +497,7 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M19 3H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2m0 16H5V5h14v14M6.2 7.7h5v1.5h-5V7.7m6.8 8.1h5v1.5h-5v-1.5m0-2.6h5v1.5h-5v-1.5M8 18h1.5v-2h2v-1.5h-2v-2H8v2H6V16h2v2m6.1-7.1 1.4-1.4 1.4 1.4 1.1-1-1.4-1.4L18 7.1 16.9 6l-1.4 1.4L14.1 6 13 7.1l1.4 1.4L13 9.9l1.1 1Z"/></svg>
|
||||
|
||||
<span class="md-ellipsis">
|
||||
Chapter 1. Introduction to Algorithms
|
||||
Chapter 1. Introduction to algorithms
|
||||
</span>
|
||||
|
||||
|
||||
@ -507,7 +513,7 @@
|
||||
<nav class="md-nav" data-md-level="1" aria-labelledby="__nav_2_label" aria-expanded="false">
|
||||
<label class="md-nav__title" for="__nav_2">
|
||||
<span class="md-nav__icon md-icon"></span>
|
||||
Chapter 1. Introduction to Algorithms
|
||||
Chapter 1. Introduction to algorithms
|
||||
</label>
|
||||
<ul class="md-nav__list" data-md-scrollfix>
|
||||
|
||||
@ -524,7 +530,7 @@
|
||||
|
||||
|
||||
<span class="md-ellipsis">
|
||||
1.1 Algorithms are Everywhere
|
||||
1.1 Algorithms are everywhere
|
||||
</span>
|
||||
|
||||
|
||||
@ -545,7 +551,7 @@
|
||||
|
||||
|
||||
<span class="md-ellipsis">
|
||||
1.2 What is an Algorithm
|
||||
1.2 What is an algorithm
|
||||
</span>
|
||||
|
||||
|
||||
@ -628,7 +634,7 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M6 2h12v6l-4 4 4 4v6H6v-6l4-4-4-4V2m10 14.5-4-4-4 4V20h8v-3.5m-4-5 4-4V4H8v3.5l4 4M10 6h4v.75l-2 2-2-2V6Z"/></svg>
|
||||
|
||||
<span class="md-ellipsis">
|
||||
Chapter 2. Complexity Analysis
|
||||
Chapter 2. Complexity analysis
|
||||
</span>
|
||||
|
||||
|
||||
@ -644,7 +650,7 @@
|
||||
<nav class="md-nav" data-md-level="1" aria-labelledby="__nav_3_label" aria-expanded="true">
|
||||
<label class="md-nav__title" for="__nav_3">
|
||||
<span class="md-nav__icon md-icon"></span>
|
||||
Chapter 2. Complexity Analysis
|
||||
Chapter 2. Complexity analysis
|
||||
</label>
|
||||
<ul class="md-nav__list" data-md-scrollfix>
|
||||
|
||||
@ -661,7 +667,7 @@
|
||||
|
||||
|
||||
<span class="md-ellipsis">
|
||||
2.1 Algorithm Efficiency Assessment
|
||||
2.1 Algorithm efficiency assessment
|
||||
</span>
|
||||
|
||||
|
||||
@ -691,7 +697,7 @@
|
||||
|
||||
|
||||
<span class="md-ellipsis">
|
||||
2.2 Iteration and Recursion
|
||||
2.2 Iteration and recursion
|
||||
</span>
|
||||
|
||||
|
||||
@ -702,7 +708,7 @@
|
||||
|
||||
|
||||
<span class="md-ellipsis">
|
||||
2.2 Iteration and Recursion
|
||||
2.2 Iteration and recursion
|
||||
</span>
|
||||
|
||||
|
||||
@ -736,7 +742,7 @@
|
||||
<li class="md-nav__item">
|
||||
<a href="#1-for-loops" class="md-nav__link">
|
||||
<span class="md-ellipsis">
|
||||
1. For Loops
|
||||
1. For loops
|
||||
</span>
|
||||
</a>
|
||||
|
||||
@ -745,7 +751,7 @@
|
||||
<li class="md-nav__item">
|
||||
<a href="#2-while-loops" class="md-nav__link">
|
||||
<span class="md-ellipsis">
|
||||
2. While Loops
|
||||
2. While loops
|
||||
</span>
|
||||
</a>
|
||||
|
||||
@ -754,7 +760,7 @@
|
||||
<li class="md-nav__item">
|
||||
<a href="#3-nested-loops" class="md-nav__link">
|
||||
<span class="md-ellipsis">
|
||||
3. Nested Loops
|
||||
3. Nested loops
|
||||
</span>
|
||||
</a>
|
||||
|
||||
@ -778,7 +784,7 @@
|
||||
<li class="md-nav__item">
|
||||
<a href="#1-call-stack" class="md-nav__link">
|
||||
<span class="md-ellipsis">
|
||||
1. Call Stack
|
||||
1. Call stack
|
||||
</span>
|
||||
</a>
|
||||
|
||||
@ -787,7 +793,7 @@
|
||||
<li class="md-nav__item">
|
||||
<a href="#2-tail-recursion" class="md-nav__link">
|
||||
<span class="md-ellipsis">
|
||||
2. Tail Recursion
|
||||
2. Tail recursion
|
||||
</span>
|
||||
</a>
|
||||
|
||||
@ -796,7 +802,7 @@
|
||||
<li class="md-nav__item">
|
||||
<a href="#3-recursion-tree" class="md-nav__link">
|
||||
<span class="md-ellipsis">
|
||||
3. Recursion Tree
|
||||
3. Recursion tree
|
||||
</span>
|
||||
</a>
|
||||
|
||||
@ -836,7 +842,7 @@
|
||||
|
||||
|
||||
<span class="md-ellipsis">
|
||||
2.3 Time Complexity
|
||||
2.3 Time complexity
|
||||
</span>
|
||||
|
||||
|
||||
@ -857,7 +863,7 @@
|
||||
|
||||
|
||||
<span class="md-ellipsis">
|
||||
2.4 Space Complexity
|
||||
2.4 Space complexity
|
||||
</span>
|
||||
|
||||
|
||||
@ -938,7 +944,7 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M11 13.5v8H3v-8h8m-2 2H5v4h4v-4M12 2l5.5 9h-11L12 2m0 3.86L10.08 9h3.84L12 5.86M17.5 13c2.5 0 4.5 2 4.5 4.5S20 22 17.5 22 13 20 13 17.5s2-4.5 4.5-4.5m0 2a2.5 2.5 0 0 0-2.5 2.5 2.5 2.5 0 0 0 2.5 2.5 2.5 2.5 0 0 0 2.5-2.5 2.5 2.5 0 0 0-2.5-2.5Z"/></svg>
|
||||
|
||||
<span class="md-ellipsis">
|
||||
Chapter 3. Data Structures
|
||||
Chapter 3. Data structures
|
||||
</span>
|
||||
|
||||
|
||||
@ -954,7 +960,7 @@
|
||||
<nav class="md-nav" data-md-level="1" aria-labelledby="__nav_4_label" aria-expanded="false">
|
||||
<label class="md-nav__title" for="__nav_4">
|
||||
<span class="md-nav__icon md-icon"></span>
|
||||
Chapter 3. Data Structures
|
||||
Chapter 3. Data structures
|
||||
</label>
|
||||
<ul class="md-nav__list" data-md-scrollfix>
|
||||
|
||||
@ -971,7 +977,7 @@
|
||||
|
||||
|
||||
<span class="md-ellipsis">
|
||||
3.1 Classification of Data Structures
|
||||
3.1 Classification of data structures
|
||||
</span>
|
||||
|
||||
|
||||
@ -992,7 +998,7 @@
|
||||
|
||||
|
||||
<span class="md-ellipsis">
|
||||
3.2 Fundamental Data Types
|
||||
3.2 Fundamental data types
|
||||
</span>
|
||||
|
||||
|
||||
@ -1013,7 +1019,7 @@
|
||||
|
||||
|
||||
<span class="md-ellipsis">
|
||||
3.3 Number Encoding *
|
||||
3.3 Number encoding *
|
||||
</span>
|
||||
|
||||
|
||||
@ -1034,7 +1040,7 @@
|
||||
|
||||
|
||||
<span class="md-ellipsis">
|
||||
3.4 Character Encoding *
|
||||
3.4 Character encoding *
|
||||
</span>
|
||||
|
||||
|
||||
@ -1115,7 +1121,7 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M3 5v14h17V5H3m4 2v2H5V7h2m-2 6v-2h2v2H5m0 2h2v2H5v-2m13 2H9v-2h9v2m0-4H9v-2h9v2m0-4H9V7h9v2Z"/></svg>
|
||||
|
||||
<span class="md-ellipsis">
|
||||
Chapter 4. Array and Linked List
|
||||
Chapter 4. Array and linked list
|
||||
</span>
|
||||
|
||||
|
||||
@ -1131,7 +1137,7 @@
|
||||
<nav class="md-nav" data-md-level="1" aria-labelledby="__nav_5_label" aria-expanded="false">
|
||||
<label class="md-nav__title" for="__nav_5">
|
||||
<span class="md-nav__icon md-icon"></span>
|
||||
Chapter 4. Array and Linked List
|
||||
Chapter 4. Array and linked list
|
||||
</label>
|
||||
<ul class="md-nav__list" data-md-scrollfix>
|
||||
|
||||
@ -1169,7 +1175,7 @@
|
||||
|
||||
|
||||
<span class="md-ellipsis">
|
||||
4.2 Linked List
|
||||
4.2 Linked list
|
||||
</span>
|
||||
|
||||
|
||||
@ -1211,7 +1217,7 @@
|
||||
|
||||
|
||||
<span class="md-ellipsis">
|
||||
4.4 Memory and Cache
|
||||
4.4 Memory and cache
|
||||
</span>
|
||||
|
||||
|
||||
@ -1290,7 +1296,7 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M17.36 20.2v-5.38h1.79V22H3v-7.18h1.8v5.38h12.56M6.77 14.32l.37-1.76 8.79 1.85-.37 1.76-8.79-1.85m1.16-4.21.76-1.61 8.14 3.78-.76 1.62-8.14-3.79m2.26-3.99 1.15-1.38 6.9 5.76-1.15 1.37-6.9-5.75m4.45-4.25L20 9.08l-1.44 1.07-5.36-7.21 1.44-1.07M6.59 18.41v-1.8h8.98v1.8H6.59Z"/></svg>
|
||||
|
||||
<span class="md-ellipsis">
|
||||
Chapter 5. Stack and Queue
|
||||
Chapter 5. Stack and queue
|
||||
</span>
|
||||
|
||||
|
||||
@ -1306,7 +1312,7 @@
|
||||
<nav class="md-nav" data-md-level="1" aria-labelledby="__nav_6_label" aria-expanded="false">
|
||||
<label class="md-nav__title" for="__nav_6">
|
||||
<span class="md-nav__icon md-icon"></span>
|
||||
Chapter 5. Stack and Queue
|
||||
Chapter 5. Stack and queue
|
||||
</label>
|
||||
<ul class="md-nav__list" data-md-scrollfix>
|
||||
|
||||
@ -1365,7 +1371,7 @@
|
||||
|
||||
|
||||
<span class="md-ellipsis">
|
||||
5.3 Double-ended Queue
|
||||
5.3 Double-ended queue
|
||||
</span>
|
||||
|
||||
|
||||
@ -1444,7 +1450,7 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M19.3 17.89c1.32-2.1.7-4.89-1.41-6.21a4.52 4.52 0 0 0-6.21 1.41C10.36 15.2 11 18 13.09 19.3c1.47.92 3.33.92 4.8 0L21 22.39 22.39 21l-3.09-3.11m-2-.62c-.98.98-2.56.97-3.54 0-.97-.98-.97-2.56.01-3.54.97-.97 2.55-.97 3.53 0 .96.99.95 2.57-.03 3.54h.03M19 4H5a2 2 0 0 0-2 2v12a2 2 0 0 0 2 2h5.81a6.3 6.3 0 0 1-1.31-2H5v-4h4.18c.16-.71.43-1.39.82-2H5V8h6v2.81a6.3 6.3 0 0 1 2-1.31V8h6v2a6.499 6.499 0 0 1 2 2V6a2 2 0 0 0-2-2Z"/></svg>
|
||||
|
||||
<span class="md-ellipsis">
|
||||
Chapter 6. Hash Table
|
||||
Chapter 6. Hash table
|
||||
</span>
|
||||
|
||||
|
||||
@ -1460,7 +1466,7 @@
|
||||
<nav class="md-nav" data-md-level="1" aria-labelledby="__nav_7_label" aria-expanded="false">
|
||||
<label class="md-nav__title" for="__nav_7">
|
||||
<span class="md-nav__icon md-icon"></span>
|
||||
Chapter 6. Hash Table
|
||||
Chapter 6. Hash table
|
||||
</label>
|
||||
<ul class="md-nav__list" data-md-scrollfix>
|
||||
|
||||
@ -1477,7 +1483,7 @@
|
||||
|
||||
|
||||
<span class="md-ellipsis">
|
||||
6.1 Hash Table
|
||||
6.1 Hash table
|
||||
</span>
|
||||
|
||||
|
||||
@ -1498,7 +1504,7 @@
|
||||
|
||||
|
||||
<span class="md-ellipsis">
|
||||
6.2 Hash Collision
|
||||
6.2 Hash collision
|
||||
</span>
|
||||
|
||||
|
||||
@ -1519,7 +1525,7 @@
|
||||
|
||||
|
||||
<span class="md-ellipsis">
|
||||
6.3 Hash Algorithm
|
||||
6.3 Hash algorithm
|
||||
</span>
|
||||
|
||||
|
||||
@ -2104,7 +2110,7 @@
|
||||
<li class="md-nav__item">
|
||||
<a href="#1-for-loops" class="md-nav__link">
|
||||
<span class="md-ellipsis">
|
||||
1. For Loops
|
||||
1. For loops
|
||||
</span>
|
||||
</a>
|
||||
|
||||
@ -2113,7 +2119,7 @@
|
||||
<li class="md-nav__item">
|
||||
<a href="#2-while-loops" class="md-nav__link">
|
||||
<span class="md-ellipsis">
|
||||
2. While Loops
|
||||
2. While loops
|
||||
</span>
|
||||
</a>
|
||||
|
||||
@ -2122,7 +2128,7 @@
|
||||
<li class="md-nav__item">
|
||||
<a href="#3-nested-loops" class="md-nav__link">
|
||||
<span class="md-ellipsis">
|
||||
3. Nested Loops
|
||||
3. Nested loops
|
||||
</span>
|
||||
</a>
|
||||
|
||||
@ -2146,7 +2152,7 @@
|
||||
<li class="md-nav__item">
|
||||
<a href="#1-call-stack" class="md-nav__link">
|
||||
<span class="md-ellipsis">
|
||||
1. Call Stack
|
||||
1. Call stack
|
||||
</span>
|
||||
</a>
|
||||
|
||||
@ -2155,7 +2161,7 @@
|
||||
<li class="md-nav__item">
|
||||
<a href="#2-tail-recursion" class="md-nav__link">
|
||||
<span class="md-ellipsis">
|
||||
2. Tail Recursion
|
||||
2. Tail recursion
|
||||
</span>
|
||||
</a>
|
||||
|
||||
@ -2164,7 +2170,7 @@
|
||||
<li class="md-nav__item">
|
||||
<a href="#3-recursion-tree" class="md-nav__link">
|
||||
<span class="md-ellipsis">
|
||||
3. Recursion Tree
|
||||
3. Recursion tree
|
||||
</span>
|
||||
</a>
|
||||
|
||||
@ -2220,11 +2226,11 @@
|
||||
|
||||
|
||||
<!-- Page content -->
|
||||
<h1 id="22-iteration-and-recursion">2.2 Iteration and Recursion<a class="headerlink" href="#22-iteration-and-recursion" title="Permanent link">¶</a></h1>
|
||||
<h1 id="22-iteration-and-recursion">2.2 Iteration and recursion<a class="headerlink" href="#22-iteration-and-recursion" title="Permanent link">¶</a></h1>
|
||||
<p>In algorithms, the repeated execution of a task is quite common and is closely related to the analysis of complexity. Therefore, before delving into the concepts of time complexity and space complexity, let's first explore how to implement repetitive tasks in programming. This involves understanding two fundamental programming control structures: iteration and recursion.</p>
|
||||
<h2 id="221-iteration">2.2.1 Iteration<a class="headerlink" href="#221-iteration" title="Permanent link">¶</a></h2>
|
||||
<p>"Iteration" is a control structure for repeatedly performing a task. In iteration, a program repeats a block of code as long as a certain condition is met until this condition is no longer satisfied.</p>
|
||||
<h3 id="1-for-loops">1. For Loops<a class="headerlink" href="#1-for-loops" title="Permanent link">¶</a></h3>
|
||||
<h3 id="1-for-loops">1. For loops<a class="headerlink" href="#1-for-loops" title="Permanent link">¶</a></h3>
|
||||
<p>The <code>for</code> loop is one of the most common forms of iteration, and <strong>it's particularly suitable when the number of iterations is known in advance</strong>.</p>
|
||||
<p>The following function uses a <code>for</code> loop to perform a summation of <span class="arithmatex">\(1 + 2 + \dots + n\)</span>, with the sum being stored in the variable <code>res</code>. It's important to note that in Python, <code>range(a, b)</code> creates an interval that is inclusive of <code>a</code> but exclusive of <code>b</code>, meaning it iterates over the range from <span class="arithmatex">\(a\)</span> up to <span class="arithmatex">\(b−1\)</span>.</p>
|
||||
<div class="tabbed-set tabbed-alternate" data-tabs="1:14"><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" /><input id="__tabbed_1_14" 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><label for="__tabbed_1_14">Zig</label></div>
|
||||
@ -2405,11 +2411,11 @@
|
||||
<div style="margin-top: 5px;"><a href="https://pythontutor.com/iframe-embed.html#code=def%20for_loop%28n%3A%20int%29%20-%3E%20int%3A%0A%20%20%20%20%22%22%22for%20%E5%BE%AA%E7%8E%AF%22%22%22%0A%20%20%20%20res%20%3D%200%0A%20%20%20%20%23%20%E5%BE%AA%E7%8E%AF%E6%B1%82%E5%92%8C%201,%202,%20...,%20n-1,%20n%0A%20%20%20%20for%20i%20in%20range%281,%20n%20%2B%201%29%3A%0A%20%20%20%20%20%20%20%20res%20%2B%3D%20i%0A%20%20%20%20return%20res%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20n%20%3D%205%0A%20%20%20%20res%20%3D%20for_loop%28n%29%0A%20%20%20%20print%28f%22%5Cnfor%20%E5%BE%AA%E7%8E%AF%E7%9A%84%E6%B1%82%E5%92%8C%E7%BB%93%E6%9E%9C%20res%20%3D%20%7Bres%7D%22%29&codeDivHeight=800&codeDivWidth=600&cumulative=false&curInstr=3&heapPrimitives=nevernest&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&" target="_blank" rel="noopener noreferrer">Full Screen ></a></div></p>
|
||||
</details>
|
||||
<p>The flowchart below represents this sum function.</p>
|
||||
<p><a class="glightbox" href="../iteration_and_recursion.assets/iteration.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Flowchart of the Sum Function" class="animation-figure" src="../iteration_and_recursion.assets/iteration.png" /></a></p>
|
||||
<p align="center"> Figure 2-1 Flowchart of the Sum Function </p>
|
||||
<p><a class="glightbox" href="../iteration_and_recursion.assets/iteration.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Flowchart of the sum function" class="animation-figure" src="../iteration_and_recursion.assets/iteration.png" /></a></p>
|
||||
<p align="center"> Figure 2-1 Flowchart of the sum function </p>
|
||||
|
||||
<p>The number of operations in this summation function is proportional to the size of the input data <span class="arithmatex">\(n\)</span>, or in other words, it has a "linear relationship." This "linear relationship" is what time complexity describes. This topic will be discussed in more detail in the next section.</p>
|
||||
<h3 id="2-while-loops">2. While Loops<a class="headerlink" href="#2-while-loops" title="Permanent link">¶</a></h3>
|
||||
<h3 id="2-while-loops">2. While loops<a class="headerlink" href="#2-while-loops" title="Permanent link">¶</a></h3>
|
||||
<p>Similar to <code>for</code> loops, <code>while</code> loops are another approach for implementing iteration. In a <code>while</code> loop, the program checks a condition at the beginning of each iteration; if the condition is true, the execution continues, otherwise, the loop ends.</p>
|
||||
<p>Below we use a <code>while</code> loop to implement the sum <span class="arithmatex">\(1 + 2 + \dots + n\)</span>.</p>
|
||||
<div class="tabbed-set tabbed-alternate" data-tabs="2:14"><input checked="checked" id="__tabbed_2_1" name="__tabbed_2" type="radio" /><input id="__tabbed_2_2" name="__tabbed_2" type="radio" /><input id="__tabbed_2_3" name="__tabbed_2" type="radio" /><input id="__tabbed_2_4" name="__tabbed_2" type="radio" /><input id="__tabbed_2_5" name="__tabbed_2" type="radio" /><input id="__tabbed_2_6" name="__tabbed_2" type="radio" /><input id="__tabbed_2_7" name="__tabbed_2" type="radio" /><input id="__tabbed_2_8" name="__tabbed_2" type="radio" /><input id="__tabbed_2_9" name="__tabbed_2" type="radio" /><input id="__tabbed_2_10" name="__tabbed_2" type="radio" /><input id="__tabbed_2_11" name="__tabbed_2" type="radio" /><input id="__tabbed_2_12" name="__tabbed_2" type="radio" /><input id="__tabbed_2_13" name="__tabbed_2" type="radio" /><input id="__tabbed_2_14" name="__tabbed_2" type="radio" /><div class="tabbed-labels"><label for="__tabbed_2_1">Python</label><label for="__tabbed_2_2">C++</label><label for="__tabbed_2_3">Java</label><label for="__tabbed_2_4">C#</label><label for="__tabbed_2_5">Go</label><label for="__tabbed_2_6">Swift</label><label for="__tabbed_2_7">JS</label><label for="__tabbed_2_8">TS</label><label for="__tabbed_2_9">Dart</label><label for="__tabbed_2_10">Rust</label><label for="__tabbed_2_11">C</label><label for="__tabbed_2_12">Kotlin</label><label for="__tabbed_2_13">Ruby</label><label for="__tabbed_2_14">Zig</label></div>
|
||||
@ -2858,7 +2864,7 @@
|
||||
<div style="margin-top: 5px;"><a href="https://pythontutor.com/iframe-embed.html#code=def%20while_loop_ii%28n%3A%20int%29%20-%3E%20int%3A%0A%20%20%20%20%22%22%22while%20%E5%BE%AA%E7%8E%AF%EF%BC%88%E4%B8%A4%E6%AC%A1%E6%9B%B4%E6%96%B0%EF%BC%89%22%22%22%0A%20%20%20%20res%20%3D%200%0A%20%20%20%20i%20%3D%201%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E6%9D%A1%E4%BB%B6%E5%8F%98%E9%87%8F%0A%20%20%20%20%23%20%E5%BE%AA%E7%8E%AF%E6%B1%82%E5%92%8C%201,%204,%2010,%20...%0A%20%20%20%20while%20i%20%3C%3D%20n%3A%0A%20%20%20%20%20%20%20%20res%20%2B%3D%20i%0A%20%20%20%20%20%20%20%20%23%20%E6%9B%B4%E6%96%B0%E6%9D%A1%E4%BB%B6%E5%8F%98%E9%87%8F%0A%20%20%20%20%20%20%20%20i%20%2B%3D%201%0A%20%20%20%20%20%20%20%20i%20*%3D%202%0A%20%20%20%20return%20res%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20n%20%3D%205%0A%20%20%20%20res%20%3D%20while_loop_ii%28n%29%0A%20%20%20%20print%28f%22%5Cnwhile%20%E5%BE%AA%E7%8E%AF%EF%BC%88%E4%B8%A4%E6%AC%A1%E6%9B%B4%E6%96%B0%EF%BC%89%E6%B1%82%E5%92%8C%E7%BB%93%E6%9E%9C%20res%20%3D%20%7Bres%7D%22%29&codeDivHeight=800&codeDivWidth=600&cumulative=false&curInstr=3&heapPrimitives=nevernest&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false" target="_blank" rel="noopener noreferrer">Full Screen ></a></div></p>
|
||||
</details>
|
||||
<p>Overall, <strong><code>for</code> loops are more concise, while <code>while</code> loops are more flexible</strong>. Both can implement iterative structures. Which one to use should be determined based on the specific requirements of the problem.</p>
|
||||
<h3 id="3-nested-loops">3. Nested Loops<a class="headerlink" href="#3-nested-loops" title="Permanent link">¶</a></h3>
|
||||
<h3 id="3-nested-loops">3. Nested loops<a class="headerlink" href="#3-nested-loops" title="Permanent link">¶</a></h3>
|
||||
<p>We can nest one loop structure within another. Below is an example using <code>for</code> loops:</p>
|
||||
<div class="tabbed-set tabbed-alternate" data-tabs="4:14"><input checked="checked" id="__tabbed_4_1" name="__tabbed_4" type="radio" /><input id="__tabbed_4_2" name="__tabbed_4" type="radio" /><input id="__tabbed_4_3" name="__tabbed_4" type="radio" /><input id="__tabbed_4_4" name="__tabbed_4" type="radio" /><input id="__tabbed_4_5" name="__tabbed_4" type="radio" /><input id="__tabbed_4_6" name="__tabbed_4" type="radio" /><input id="__tabbed_4_7" name="__tabbed_4" type="radio" /><input id="__tabbed_4_8" name="__tabbed_4" type="radio" /><input id="__tabbed_4_9" name="__tabbed_4" type="radio" /><input id="__tabbed_4_10" name="__tabbed_4" type="radio" /><input id="__tabbed_4_11" name="__tabbed_4" type="radio" /><input id="__tabbed_4_12" name="__tabbed_4" type="radio" /><input id="__tabbed_4_13" name="__tabbed_4" type="radio" /><input id="__tabbed_4_14" name="__tabbed_4" type="radio" /><div class="tabbed-labels"><label for="__tabbed_4_1">Python</label><label for="__tabbed_4_2">C++</label><label for="__tabbed_4_3">Java</label><label for="__tabbed_4_4">C#</label><label for="__tabbed_4_5">Go</label><label for="__tabbed_4_6">Swift</label><label for="__tabbed_4_7">JS</label><label for="__tabbed_4_8">TS</label><label for="__tabbed_4_9">Dart</label><label for="__tabbed_4_10">Rust</label><label for="__tabbed_4_11">C</label><label for="__tabbed_4_12">Kotlin</label><label for="__tabbed_4_13">Ruby</label><label for="__tabbed_4_14">Zig</label></div>
|
||||
<div class="tabbed-content">
|
||||
@ -3086,8 +3092,8 @@
|
||||
<div style="margin-top: 5px;"><a href="https://pythontutor.com/iframe-embed.html#code=def%20nested_for_loop%28n%3A%20int%29%20-%3E%20str%3A%0A%20%20%20%20%22%22%22%E5%8F%8C%E5%B1%82%20for%20%E5%BE%AA%E7%8E%AF%22%22%22%0A%20%20%20%20res%20%3D%20%22%22%0A%20%20%20%20%23%20%E5%BE%AA%E7%8E%AF%20i%20%3D%201,%202,%20...,%20n-1,%20n%0A%20%20%20%20for%20i%20in%20range%281,%20n%20%2B%201%29%3A%0A%20%20%20%20%20%20%20%20%23%20%E5%BE%AA%E7%8E%AF%20j%20%3D%201,%202,%20...,%20n-1,%20n%0A%20%20%20%20%20%20%20%20for%20j%20in%20range%281,%20n%20%2B%201%29%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20res%20%2B%3D%20f%22%28%7Bi%7D,%20%7Bj%7D%29,%20%22%0A%20%20%20%20return%20res%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20n%20%3D%205%0A%20%20%20%20res%20%3D%20nested_for_loop%28n%29%0A%20%20%20%20print%28f%22%5Cn%E5%8F%8C%E5%B1%82%20for%20%E5%BE%AA%E7%8E%AF%E7%9A%84%E9%81%8D%E5%8E%86%E7%BB%93%E6%9E%9C%20%7Bres%7D%22%29&codeDivHeight=800&codeDivWidth=600&cumulative=false&curInstr=3&heapPrimitives=nevernest&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false" target="_blank" rel="noopener noreferrer">Full Screen ></a></div></p>
|
||||
</details>
|
||||
<p>The flowchart below represents this nested loop.</p>
|
||||
<p><a class="glightbox" href="../iteration_and_recursion.assets/nested_iteration.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Flowchart of the Nested Loop" class="animation-figure" src="../iteration_and_recursion.assets/nested_iteration.png" /></a></p>
|
||||
<p align="center"> Figure 2-2 Flowchart of the Nested Loop </p>
|
||||
<p><a class="glightbox" href="../iteration_and_recursion.assets/nested_iteration.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Flowchart of the nested loop" class="animation-figure" src="../iteration_and_recursion.assets/nested_iteration.png" /></a></p>
|
||||
<p align="center"> Figure 2-2 Flowchart of the nested loop </p>
|
||||
|
||||
<p>In such cases, the number of operations of the function is proportional to <span class="arithmatex">\(n^2\)</span>, meaning the algorithm's runtime and the size of the input data <span class="arithmatex">\(n\)</span> has a 'quadratic relationship.'</p>
|
||||
<p>We can further increase the complexity by adding more nested loops, each level of nesting effectively "increasing the dimension," which raises the time complexity to "cubic," "quartic," and so on.</p>
|
||||
@ -3295,8 +3301,8 @@
|
||||
<div style="margin-top: 5px;"><a href="https://pythontutor.com/iframe-embed.html#code=def%20recur%28n%3A%20int%29%20-%3E%20int%3A%0A%20%20%20%20%22%22%22%E9%80%92%E5%BD%92%22%22%22%0A%20%20%20%20%23%20%E7%BB%88%E6%AD%A2%E6%9D%A1%E4%BB%B6%0A%20%20%20%20if%20n%20%3D%3D%201%3A%0A%20%20%20%20%20%20%20%20return%201%0A%20%20%20%20%23%20%E9%80%92%EF%BC%9A%E9%80%92%E5%BD%92%E8%B0%83%E7%94%A8%0A%20%20%20%20res%20%3D%20recur%28n%20-%201%29%0A%20%20%20%20%23%20%E5%BD%92%EF%BC%9A%E8%BF%94%E5%9B%9E%E7%BB%93%E6%9E%9C%0A%20%20%20%20return%20n%20%2B%20res%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20n%20%3D%205%0A%20%20%20%20res%20%3D%20recur%28n%29%0A%20%20%20%20print%28f%22%5Cn%E9%80%92%E5%BD%92%E5%87%BD%E6%95%B0%E7%9A%84%E6%B1%82%E5%92%8C%E7%BB%93%E6%9E%9C%20res%20%3D%20%7Bres%7D%22%29&codeDivHeight=800&codeDivWidth=600&cumulative=false&curInstr=3&heapPrimitives=nevernest&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false" target="_blank" rel="noopener noreferrer">Full Screen ></a></div></p>
|
||||
</details>
|
||||
<p>The Figure 2-3 shows the recursive process of this function.</p>
|
||||
<p><a class="glightbox" href="../iteration_and_recursion.assets/recursion_sum.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Recursive Process of the Sum Function" class="animation-figure" src="../iteration_and_recursion.assets/recursion_sum.png" /></a></p>
|
||||
<p align="center"> Figure 2-3 Recursive Process of the Sum Function </p>
|
||||
<p><a class="glightbox" href="../iteration_and_recursion.assets/recursion_sum.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Recursive process of the sum function" class="animation-figure" src="../iteration_and_recursion.assets/recursion_sum.png" /></a></p>
|
||||
<p align="center"> Figure 2-3 Recursive process of the sum function </p>
|
||||
|
||||
<p>Although iteration and recursion can achieve the same results from a computational standpoint, <strong>they represent two entirely different paradigms of thinking and problem-solving</strong>.</p>
|
||||
<ul>
|
||||
@ -3308,22 +3314,22 @@
|
||||
<li><strong>Iteration</strong>: In this approach, we simulate the summation process within a loop. Starting from <span class="arithmatex">\(1\)</span> and traversing to <span class="arithmatex">\(n\)</span>, we perform the summation operation in each iteration to eventually compute <span class="arithmatex">\(f(n)\)</span>.</li>
|
||||
<li><strong>Recursion</strong>: Here, the problem is broken down into a sub-problem: <span class="arithmatex">\(f(n) = n + f(n-1)\)</span>. This decomposition continues recursively until reaching the base case, <span class="arithmatex">\(f(1) = 1\)</span>, at which point the recursion terminates.</li>
|
||||
</ul>
|
||||
<h3 id="1-call-stack">1. Call Stack<a class="headerlink" href="#1-call-stack" title="Permanent link">¶</a></h3>
|
||||
<h3 id="1-call-stack">1. Call stack<a class="headerlink" href="#1-call-stack" title="Permanent link">¶</a></h3>
|
||||
<p>Every time a recursive function calls itself, the system allocates memory for the newly initiated function to store local variables, the return address, and other relevant information. This leads to two primary outcomes.</p>
|
||||
<ul>
|
||||
<li>The function's context data is stored in a memory area called "stack frame space" and is only released after the function returns. Therefore, <strong>recursion generally consumes more memory space than iteration</strong>.</li>
|
||||
<li>Recursive calls introduce additional overhead. <strong>Hence, recursion is usually less time-efficient than loops.</strong></li>
|
||||
</ul>
|
||||
<p>As shown in the Figure 2-4 , there are <span class="arithmatex">\(n\)</span> unreturned recursive functions before triggering the termination condition, indicating a <strong>recursion depth of <span class="arithmatex">\(n\)</span></strong>.</p>
|
||||
<p><a class="glightbox" href="../iteration_and_recursion.assets/recursion_sum_depth.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Recursion Call Depth" class="animation-figure" src="../iteration_and_recursion.assets/recursion_sum_depth.png" /></a></p>
|
||||
<p align="center"> Figure 2-4 Recursion Call Depth </p>
|
||||
<p><a class="glightbox" href="../iteration_and_recursion.assets/recursion_sum_depth.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Recursion call depth" class="animation-figure" src="../iteration_and_recursion.assets/recursion_sum_depth.png" /></a></p>
|
||||
<p align="center"> Figure 2-4 Recursion call depth </p>
|
||||
|
||||
<p>In practice, the depth of recursion allowed by programming languages is usually limited, and excessively deep recursion can 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>
|
||||
<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 performs its recursive call as the very last step before returning,</strong> it can be optimized by the compiler or interpreter to be as space-efficient as iteration. This scenario is known as "tail recursion."</p>
|
||||
<ul>
|
||||
<li><strong>Regular Recursion</strong>: In standard recursion, when the function returns to the previous level, it continues to execute more code, requiring the system to save the context of the previous call.</li>
|
||||
<li><strong>Tail Recursion</strong>: Here, the recursive call is the final operation before the function returns. This means that upon returning to the previous level, no further actions are needed, so the system does not need to save the context of the previous level.</li>
|
||||
<li><strong>Regular recursion</strong>: In standard recursion, when the function returns to the previous level, it continues to execute more code, requiring the system to save the context of the previous call.</li>
|
||||
<li><strong>Tail recursion</strong>: Here, the recursive call is the final operation before the function returns. This means that upon returning to the previous level, no further actions are needed, so the system does not need to save the context of the previous level.</li>
|
||||
</ul>
|
||||
<p>For example, in calculating <span class="arithmatex">\(1 + 2 + \dots + n\)</span>, we can make the result variable <code>res</code> a parameter of the function, thereby achieving tail recursion:</p>
|
||||
<div class="tabbed-set tabbed-alternate" data-tabs="6:14"><input checked="checked" id="__tabbed_6_1" name="__tabbed_6" type="radio" /><input id="__tabbed_6_2" name="__tabbed_6" type="radio" /><input id="__tabbed_6_3" name="__tabbed_6" type="radio" /><input id="__tabbed_6_4" name="__tabbed_6" type="radio" /><input id="__tabbed_6_5" name="__tabbed_6" type="radio" /><input id="__tabbed_6_6" name="__tabbed_6" type="radio" /><input id="__tabbed_6_7" name="__tabbed_6" type="radio" /><input id="__tabbed_6_8" name="__tabbed_6" type="radio" /><input id="__tabbed_6_9" name="__tabbed_6" type="radio" /><input id="__tabbed_6_10" name="__tabbed_6" type="radio" /><input id="__tabbed_6_11" name="__tabbed_6" type="radio" /><input id="__tabbed_6_12" name="__tabbed_6" type="radio" /><input id="__tabbed_6_13" name="__tabbed_6" type="radio" /><input id="__tabbed_6_14" name="__tabbed_6" type="radio" /><div class="tabbed-labels"><label for="__tabbed_6_1">Python</label><label for="__tabbed_6_2">C++</label><label for="__tabbed_6_3">Java</label><label for="__tabbed_6_4">C#</label><label for="__tabbed_6_5">Go</label><label for="__tabbed_6_6">Swift</label><label for="__tabbed_6_7">JS</label><label for="__tabbed_6_8">TS</label><label for="__tabbed_6_9">Dart</label><label for="__tabbed_6_10">Rust</label><label for="__tabbed_6_11">C</label><label for="__tabbed_6_12">Kotlin</label><label for="__tabbed_6_13">Ruby</label><label for="__tabbed_6_14">Zig</label></div>
|
||||
@ -3491,17 +3497,17 @@
|
||||
</details>
|
||||
<p>The execution process of tail recursion is shown in the following figure. Comparing regular recursion and tail recursion, the point of the summation operation is different.</p>
|
||||
<ul>
|
||||
<li><strong>Regular Recursion</strong>: The summation operation occurs during the "returning" phase, requiring another summation after each layer returns.</li>
|
||||
<li><strong>Tail Recursion</strong>: The summation operation occurs during the "calling" phase, and the "returning" phase only involves returning through each layer.</li>
|
||||
<li><strong>Regular recursion</strong>: The summation operation occurs during the "returning" phase, requiring another summation after each layer returns.</li>
|
||||
<li><strong>Tail recursion</strong>: The summation operation occurs during the "calling" phase, and the "returning" phase only involves returning through each layer.</li>
|
||||
</ul>
|
||||
<p><a class="glightbox" href="../iteration_and_recursion.assets/tail_recursion_sum.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Tail Recursion Process" class="animation-figure" src="../iteration_and_recursion.assets/tail_recursion_sum.png" /></a></p>
|
||||
<p align="center"> Figure 2-5 Tail Recursion Process </p>
|
||||
<p><a class="glightbox" href="../iteration_and_recursion.assets/tail_recursion_sum.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Tail recursion process" class="animation-figure" src="../iteration_and_recursion.assets/tail_recursion_sum.png" /></a></p>
|
||||
<p align="center"> Figure 2-5 Tail recursion process </p>
|
||||
|
||||
<div class="admonition tip">
|
||||
<p class="admonition-title">Tip</p>
|
||||
<p>Note that many compilers or interpreters do not support tail recursion optimization. For example, Python does not support tail recursion optimization by default, so even if the function is in the form of tail recursion, it may still encounter stack overflow issues.</p>
|
||||
</div>
|
||||
<h3 id="3-recursion-tree">3. Recursion Tree<a class="headerlink" href="#3-recursion-tree" title="Permanent link">¶</a></h3>
|
||||
<h3 id="3-recursion-tree">3. Recursion tree<a class="headerlink" href="#3-recursion-tree" title="Permanent link">¶</a></h3>
|
||||
<p>When dealing with algorithms related to "divide and conquer", recursion often offers a more intuitive approach and more readable code than iteration. Take the "Fibonacci sequence" as an example.</p>
|
||||
<div class="admonition question">
|
||||
<p class="admonition-title">Question</p>
|
||||
@ -3704,8 +3710,8 @@
|
||||
<div style="margin-top: 5px;"><a href="https://pythontutor.com/iframe-embed.html#code=def%20fib%28n%3A%20int%29%20-%3E%20int%3A%0A%20%20%20%20%22%22%22%E6%96%90%E6%B3%A2%E9%82%A3%E5%A5%91%E6%95%B0%E5%88%97%EF%BC%9A%E9%80%92%E5%BD%92%22%22%22%0A%20%20%20%20%23%20%E7%BB%88%E6%AD%A2%E6%9D%A1%E4%BB%B6%20f%281%29%20%3D%200,%20f%282%29%20%3D%201%0A%20%20%20%20if%20n%20%3D%3D%201%20or%20n%20%3D%3D%202%3A%0A%20%20%20%20%20%20%20%20return%20n%20-%201%0A%20%20%20%20%23%20%E9%80%92%E5%BD%92%E8%B0%83%E7%94%A8%20f%28n%29%20%3D%20f%28n-1%29%20%2B%20f%28n-2%29%0A%20%20%20%20res%20%3D%20fib%28n%20-%201%29%20%2B%20fib%28n%20-%202%29%0A%20%20%20%20%23%20%E8%BF%94%E5%9B%9E%E7%BB%93%E6%9E%9C%20f%28n%29%0A%20%20%20%20return%20res%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20n%20%3D%205%0A%20%20%20%20res%20%3D%20fib%28n%29%0A%20%20%20%20print%28f%22%5Cn%E6%96%90%E6%B3%A2%E9%82%A3%E5%A5%91%E6%95%B0%E5%88%97%E7%9A%84%E7%AC%AC%20%7Bn%7D%20%E9%A1%B9%E4%B8%BA%20%7Bres%7D%22%29&codeDivHeight=800&codeDivWidth=600&cumulative=false&curInstr=3&heapPrimitives=nevernest&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false" target="_blank" rel="noopener noreferrer">Full Screen ></a></div></p>
|
||||
</details>
|
||||
<p>Observing the above code, we see that it recursively calls two functions within itself, <strong>meaning that one call generates two branching calls</strong>. As illustrated below, this continuous recursive calling eventually creates a "recursion tree" with a depth of <span class="arithmatex">\(n\)</span>.</p>
|
||||
<p><a class="glightbox" href="../iteration_and_recursion.assets/recursion_tree.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Fibonacci Sequence Recursion Tree" class="animation-figure" src="../iteration_and_recursion.assets/recursion_tree.png" /></a></p>
|
||||
<p align="center"> Figure 2-6 Fibonacci Sequence Recursion Tree </p>
|
||||
<p><a class="glightbox" href="../iteration_and_recursion.assets/recursion_tree.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Fibonacci sequence recursion tree" class="animation-figure" src="../iteration_and_recursion.assets/recursion_tree.png" /></a></p>
|
||||
<p align="center"> Figure 2-6 Fibonacci sequence recursion tree </p>
|
||||
|
||||
<p>Fundamentally, recursion embodies the paradigm of "breaking down a problem into smaller sub-problems." This divide-and-conquer strategy is crucial.</p>
|
||||
<ul>
|
||||
@ -3714,7 +3720,7 @@
|
||||
</ul>
|
||||
<h2 id="223-comparison">2.2.3 Comparison<a class="headerlink" href="#223-comparison" title="Permanent link">¶</a></h2>
|
||||
<p>Summarizing the above content, the following table shows the differences between iteration and recursion in terms of implementation, performance, and applicability.</p>
|
||||
<p align="center"> Table: Comparison of Iteration and Recursion Characteristics </p>
|
||||
<p align="center"> Table: Comparison of iteration and recursion characteristics </p>
|
||||
|
||||
<div class="center-table">
|
||||
<table>
|
||||
@ -4092,7 +4098,7 @@ aria-label="Footer"
|
||||
<a
|
||||
href="../performance_evaluation/"
|
||||
class="md-footer__link md-footer__link--prev"
|
||||
aria-label="Previous: 2.1 Algorithm Efficiency Assessment"
|
||||
aria-label="Previous: 2.1 Algorithm efficiency assessment"
|
||||
rel="prev"
|
||||
>
|
||||
<div class="md-footer__button md-icon">
|
||||
@ -4104,7 +4110,7 @@ aria-label="Footer"
|
||||
Previous
|
||||
</span>
|
||||
<div class="md-ellipsis">
|
||||
2.1 Algorithm Efficiency Assessment
|
||||
2.1 Algorithm efficiency assessment
|
||||
</div>
|
||||
</div>
|
||||
</a>
|
||||
@ -4116,7 +4122,7 @@ aria-label="Footer"
|
||||
<a
|
||||
href="../time_complexity/"
|
||||
class="md-footer__link md-footer__link--next"
|
||||
aria-label="Next: 2.3 Time Complexity"
|
||||
aria-label="Next: 2.3 Time complexity"
|
||||
rel="next"
|
||||
>
|
||||
<div class="md-footer__title">
|
||||
@ -4124,7 +4130,7 @@ aria-label="Footer"
|
||||
Next
|
||||
</span>
|
||||
<div class="md-ellipsis">
|
||||
2.3 Time Complexity
|
||||
2.3 Time complexity
|
||||
</div>
|
||||
</div>
|
||||
<div class="md-footer__button md-icon">
|
||||
@ -4217,7 +4223,7 @@ aria-label="Footer"
|
||||
<nav class="md-footer__inner md-grid" aria-label="Footer" >
|
||||
|
||||
|
||||
<a href="../performance_evaluation/" class="md-footer__link md-footer__link--prev" aria-label="Previous: 2.1 Algorithm Efficiency Assessment">
|
||||
<a href="../performance_evaluation/" class="md-footer__link md-footer__link--prev" aria-label="Previous: 2.1 Algorithm efficiency assessment">
|
||||
<div class="md-footer__button md-icon">
|
||||
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
|
||||
@ -4227,20 +4233,20 @@ aria-label="Footer"
|
||||
Previous
|
||||
</span>
|
||||
<div class="md-ellipsis">
|
||||
2.1 Algorithm Efficiency Assessment
|
||||
2.1 Algorithm efficiency assessment
|
||||
</div>
|
||||
</div>
|
||||
</a>
|
||||
|
||||
|
||||
|
||||
<a href="../time_complexity/" class="md-footer__link md-footer__link--next" aria-label="Next: 2.3 Time Complexity">
|
||||
<a href="../time_complexity/" class="md-footer__link md-footer__link--next" aria-label="Next: 2.3 Time complexity">
|
||||
<div class="md-footer__title">
|
||||
<span class="md-footer__direction">
|
||||
Next
|
||||
</span>
|
||||
<div class="md-ellipsis">
|
||||
2.3 Time Complexity
|
||||
2.3 Time complexity
|
||||
</div>
|
||||
</div>
|
||||
<div class="md-footer__button md-icon">
|
||||
|
Reference in New Issue
Block a user