This commit is contained in:
krahets
2024-04-06 03:02:26 +08:00
parent 5988d20958
commit a263b839b9
875 changed files with 484736 additions and 2893 deletions
@@ -26,7 +26,7 @@
<title>2.1 Algorithm Efficiency Assessment - Hello Algo</title>
<title>2.1 Algorithm efficiency assessment - Hello Algo</title>
@@ -153,7 +153,7 @@
<div class="md-header__topic" data-md-component="header-topic">
<span class="md-ellipsis">
2.1 Algorithm Efficiency Assessment
2.1 Algorithm efficiency assessment
</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>
@@ -670,7 +676,7 @@
<span class="md-ellipsis">
2.1 Algorithm Efficiency Assessment
2.1 Algorithm efficiency assessment
</span>
@@ -681,7 +687,7 @@
<span class="md-ellipsis">
2.1 Algorithm Efficiency Assessment
2.1 Algorithm efficiency assessment
</span>
@@ -705,7 +711,7 @@
<li class="md-nav__item">
<a href="#211-actual-testing" class="md-nav__link">
<span class="md-ellipsis">
2.1.1 &nbsp; Actual Testing
2.1.1 &nbsp; Actual testing
</span>
</a>
@@ -714,7 +720,7 @@
<li class="md-nav__item">
<a href="#212-theoretical-estimation" class="md-nav__link">
<span class="md-ellipsis">
2.1.2 &nbsp; Theoretical Estimation
2.1.2 &nbsp; Theoretical estimation
</span>
</a>
@@ -740,7 +746,7 @@
<span class="md-ellipsis">
2.2 Iteration and Recursion
2.2 Iteration and recursion
</span>
@@ -761,7 +767,7 @@
<span class="md-ellipsis">
2.3 Time Complexity
2.3 Time complexity
</span>
@@ -782,7 +788,7 @@
<span class="md-ellipsis">
2.4 Space Complexity
2.4 Space complexity
</span>
@@ -863,7 +869,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>
@@ -879,7 +885,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>
@@ -896,7 +902,7 @@
<span class="md-ellipsis">
3.1 Classification of Data Structures
3.1 Classification of data structures
</span>
@@ -917,7 +923,7 @@
<span class="md-ellipsis">
3.2 Fundamental Data Types
3.2 Fundamental data types
</span>
@@ -938,7 +944,7 @@
<span class="md-ellipsis">
3.3 Number Encoding *
3.3 Number encoding *
</span>
@@ -959,7 +965,7 @@
<span class="md-ellipsis">
3.4 Character Encoding *
3.4 Character encoding *
</span>
@@ -1040,7 +1046,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>
@@ -1056,7 +1062,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>
@@ -1094,7 +1100,7 @@
<span class="md-ellipsis">
4.2 Linked List
4.2 Linked list
</span>
@@ -1136,7 +1142,7 @@
<span class="md-ellipsis">
4.4 Memory and Cache
4.4 Memory and cache
</span>
@@ -1215,7 +1221,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>
@@ -1231,7 +1237,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>
@@ -1290,7 +1296,7 @@
<span class="md-ellipsis">
5.3 Double-ended Queue
5.3 Double-ended queue
</span>
@@ -1369,7 +1375,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>
@@ -1385,7 +1391,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>
@@ -1402,7 +1408,7 @@
<span class="md-ellipsis">
6.1 Hash Table
6.1 Hash table
</span>
@@ -1423,7 +1429,7 @@
<span class="md-ellipsis">
6.2 Hash Collision
6.2 Hash collision
</span>
@@ -1444,7 +1450,7 @@
<span class="md-ellipsis">
6.3 Hash Algorithm
6.3 Hash algorithm
</span>
@@ -2019,7 +2025,7 @@
<li class="md-nav__item">
<a href="#211-actual-testing" class="md-nav__link">
<span class="md-ellipsis">
2.1.1 &nbsp; Actual Testing
2.1.1 &nbsp; Actual testing
</span>
</a>
@@ -2028,7 +2034,7 @@
<li class="md-nav__item">
<a href="#212-theoretical-estimation" class="md-nav__link">
<span class="md-ellipsis">
2.1.2 &nbsp; Theoretical Estimation
2.1.2 &nbsp; Theoretical estimation
</span>
</a>
@@ -2070,7 +2076,7 @@
<!-- Page content -->
<h1 id="21-algorithm-efficiency-assessment">2.1 &nbsp; Algorithm Efficiency Assessment<a class="headerlink" href="#21-algorithm-efficiency-assessment" title="Permanent link">&para;</a></h1>
<h1 id="21-algorithm-efficiency-assessment">2.1 &nbsp; Algorithm efficiency assessment<a class="headerlink" href="#21-algorithm-efficiency-assessment" title="Permanent link">&para;</a></h1>
<p>In algorithm design, we pursue the following two objectives in sequence.</p>
<ol>
<li><strong>Finding a Solution to the Problem</strong>: The algorithm should reliably find the correct solution within the stipulated range of inputs.</li>
@@ -2078,16 +2084,16 @@
</ol>
<p>In other words, under the premise of being able to solve the problem, algorithm efficiency has become the main criterion for evaluating the merits of an algorithm, which includes the following two dimensions.</p>
<ul>
<li><strong>Time Efficiency</strong>: The speed at which an algorithm runs.</li>
<li><strong>Space Efficiency</strong>: The size of the memory space occupied by an algorithm.</li>
<li><strong>Time efficiency</strong>: The speed at which an algorithm runs.</li>
<li><strong>Space efficiency</strong>: The size of the memory space occupied by an algorithm.</li>
</ul>
<p>In short, <strong>our goal is to design data structures and algorithms that are both fast and memory-efficient</strong>. Effectively assessing algorithm efficiency is crucial because only then can we compare various algorithms and guide the process of algorithm design and optimization.</p>
<p>There are mainly two methods of efficiency assessment: actual testing and theoretical estimation.</p>
<h2 id="211-actual-testing">2.1.1 &nbsp; Actual Testing<a class="headerlink" href="#211-actual-testing" title="Permanent link">&para;</a></h2>
<h2 id="211-actual-testing">2.1.1 &nbsp; Actual testing<a class="headerlink" href="#211-actual-testing" title="Permanent link">&para;</a></h2>
<p>Suppose we have algorithms <code>A</code> and <code>B</code>, both capable of solving the same problem, and we need to compare their efficiencies. The most direct method is to use a computer to run these two algorithms and monitor and record their runtime and memory usage. This assessment method reflects the actual situation but has significant limitations.</p>
<p>On one hand, <strong>it's difficult to eliminate interference from the testing environment</strong>. Hardware configurations can affect algorithm performance. For example, algorithm <code>A</code> might run faster than <code>B</code> on one computer, but the opposite result may occur on another computer with different configurations. This means we would need to test on a variety of machines to calculate average efficiency, which is impractical.</p>
<p>On the other hand, <strong>conducting a full test is very resource-intensive</strong>. As the volume of input data changes, the efficiency of the algorithms may vary. For example, with smaller data volumes, algorithm <code>A</code> might run faster than <code>B</code>, but the opposite might be true with larger data volumes. Therefore, to draw convincing conclusions, we need to test a wide range of input data sizes, which requires significant computational resources.</p>
<h2 id="212-theoretical-estimation">2.1.2 &nbsp; Theoretical Estimation<a class="headerlink" href="#212-theoretical-estimation" title="Permanent link">&para;</a></h2>
<h2 id="212-theoretical-estimation">2.1.2 &nbsp; Theoretical estimation<a class="headerlink" href="#212-theoretical-estimation" title="Permanent link">&para;</a></h2>
<p>Due to the significant limitations of actual testing, we can consider evaluating algorithm efficiency solely through calculations. This estimation method is known as "asymptotic complexity analysis," or simply "complexity analysis."</p>
<p>Complexity analysis reflects the relationship between the time and space resources required for algorithm execution and the size of the input data. <strong>It describes the trend of growth in the time and space required by the algorithm as the size of the input data increases</strong>. This definition might sound complex, but we can break it down into three key points to understand it better.</p>
<ul>
@@ -2129,7 +2135,7 @@ aria-label="Footer"
<a
href="../"
class="md-footer__link md-footer__link--prev"
aria-label="Previous: Chapter 2. &amp;nbsp; Complexity Analysis"
aria-label="Previous: Chapter 2. &amp;nbsp; Complexity analysis"
rel="prev"
>
<div class="md-footer__button md-icon">
@@ -2141,7 +2147,7 @@ aria-label="Footer"
Previous
</span>
<div class="md-ellipsis">
Chapter 2. &nbsp; Complexity Analysis
Chapter 2. &nbsp; Complexity analysis
</div>
</div>
</a>
@@ -2153,7 +2159,7 @@ aria-label="Footer"
<a
href="../iteration_and_recursion/"
class="md-footer__link md-footer__link--next"
aria-label="Next: 2.2 Iteration and Recursion"
aria-label="Next: 2.2 Iteration and recursion"
rel="next"
>
<div class="md-footer__title">
@@ -2161,7 +2167,7 @@ aria-label="Footer"
Next
</span>
<div class="md-ellipsis">
2.2 Iteration and Recursion
2.2 Iteration and recursion
</div>
</div>
<div class="md-footer__button md-icon">
@@ -2254,7 +2260,7 @@ aria-label="Footer"
<nav class="md-footer__inner md-grid" aria-label="Footer" >
<a href="../" class="md-footer__link md-footer__link--prev" aria-label="Previous: Chapter 2. &amp;nbsp; Complexity Analysis">
<a href="../" class="md-footer__link md-footer__link--prev" aria-label="Previous: Chapter 2. &amp;nbsp; Complexity analysis">
<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>
@@ -2264,20 +2270,20 @@ aria-label="Footer"
Previous
</span>
<div class="md-ellipsis">
Chapter 2. &nbsp; Complexity Analysis
Chapter 2. &nbsp; Complexity analysis
</div>
</div>
</a>
<a href="../iteration_and_recursion/" class="md-footer__link md-footer__link--next" aria-label="Next: 2.2 Iteration and Recursion">
<a href="../iteration_and_recursion/" class="md-footer__link md-footer__link--next" aria-label="Next: 2.2 Iteration and recursion">
<div class="md-footer__title">
<span class="md-footer__direction">
Next
</span>
<div class="md-ellipsis">
2.2 Iteration and Recursion
2.2 Iteration and recursion
</div>
</div>
<div class="md-footer__button md-icon">