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
+49 -43
View File
@@ -26,7 +26,7 @@
<title>Chapter 2.   Complexity Analysis - Hello Algo</title>
<title>Chapter 2.   Complexity analysis - Hello Algo</title>
@@ -153,7 +153,7 @@
<div class="md-header__topic" data-md-component="header-topic">
<span class="md-ellipsis">
Chapter 2. &nbsp; Complexity Analysis
Chapter 2. &nbsp; Complexity analysis
</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>
@@ -682,7 +688,7 @@
<span class="md-ellipsis">
2.2 Iteration and Recursion
2.2 Iteration and recursion
</span>
@@ -703,7 +709,7 @@
<span class="md-ellipsis">
2.3 Time Complexity
2.3 Time complexity
</span>
@@ -724,7 +730,7 @@
<span class="md-ellipsis">
2.4 Space Complexity
2.4 Space complexity
</span>
@@ -805,7 +811,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>
@@ -821,7 +827,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>
@@ -838,7 +844,7 @@
<span class="md-ellipsis">
3.1 Classification of Data Structures
3.1 Classification of data structures
</span>
@@ -859,7 +865,7 @@
<span class="md-ellipsis">
3.2 Fundamental Data Types
3.2 Fundamental data types
</span>
@@ -880,7 +886,7 @@
<span class="md-ellipsis">
3.3 Number Encoding *
3.3 Number encoding *
</span>
@@ -901,7 +907,7 @@
<span class="md-ellipsis">
3.4 Character Encoding *
3.4 Character encoding *
</span>
@@ -982,7 +988,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>
@@ -998,7 +1004,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>
@@ -1036,7 +1042,7 @@
<span class="md-ellipsis">
4.2 Linked List
4.2 Linked list
</span>
@@ -1078,7 +1084,7 @@
<span class="md-ellipsis">
4.4 Memory and Cache
4.4 Memory and cache
</span>
@@ -1157,7 +1163,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>
@@ -1173,7 +1179,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>
@@ -1232,7 +1238,7 @@
<span class="md-ellipsis">
5.3 Double-ended Queue
5.3 Double-ended queue
</span>
@@ -1311,7 +1317,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>
@@ -1327,7 +1333,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>
@@ -1344,7 +1350,7 @@
<span class="md-ellipsis">
6.1 Hash Table
6.1 Hash table
</span>
@@ -1365,7 +1371,7 @@
<span class="md-ellipsis">
6.2 Hash Collision
6.2 Hash collision
</span>
@@ -1386,7 +1392,7 @@
<span class="md-ellipsis">
6.3 Hash Algorithm
6.3 Hash algorithm
</span>
@@ -2003,8 +2009,8 @@
<!-- Page content -->
<h1 id="chapter-2-complexity-analysis">Chapter 2. &nbsp; Complexity Analysis<a class="headerlink" href="#chapter-2-complexity-analysis" title="Permanent link">&para;</a></h1>
<p><a class="glightbox" href="../assets/covers/chapter_complexity_analysis.jpg" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="complexity_analysis" class="cover-image" src="../assets/covers/chapter_complexity_analysis.jpg" /></a></p>
<h1 id="chapter-2-complexity-analysis">Chapter 2. &nbsp; Complexity analysis<a class="headerlink" href="#chapter-2-complexity-analysis" title="Permanent link">&para;</a></h1>
<p><a class="glightbox" href="../assets/covers/chapter_complexity_analysis.jpg" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Complexity analysis" class="cover-image" src="../assets/covers/chapter_complexity_analysis.jpg" /></a></p>
<div class="admonition abstract">
<p class="admonition-title">Abstract</p>
<p>Complexity analysis is like a space-time navigator in the vast universe of algorithms.</p>
@@ -2012,10 +2018,10 @@
</div>
<h2 id="chapter-contents">Chapter Contents<a class="headerlink" href="#chapter-contents" title="Permanent link">&para;</a></h2>
<ul>
<li><a href="https://www.hello-algo.com/en/chapter_computational_complexity/performance_evaluation/">2.1 &nbsp; Algorithm Efficiency Assessment</a></li>
<li><a href="https://www.hello-algo.com/en/chapter_computational_complexity/iteration_and_recursion/">2.2 &nbsp; Iteration and Recursion</a></li>
<li><a href="https://www.hello-algo.com/en/chapter_computational_complexity/time_complexity/">2.3 &nbsp; Time Complexity</a></li>
<li><a href="https://www.hello-algo.com/en/chapter_computational_complexity/space_complexity/">2.4 &nbsp; Space Complexity</a></li>
<li><a href="https://www.hello-algo.com/en/chapter_computational_complexity/performance_evaluation/">2.1 &nbsp; Algorithm efficiency assessment</a></li>
<li><a href="https://www.hello-algo.com/en/chapter_computational_complexity/iteration_and_recursion/">2.2 &nbsp; Iteration and recursion</a></li>
<li><a href="https://www.hello-algo.com/en/chapter_computational_complexity/time_complexity/">2.3 &nbsp; Time complexity</a></li>
<li><a href="https://www.hello-algo.com/en/chapter_computational_complexity/space_complexity/">2.4 &nbsp; Space complexity</a></li>
<li><a href="https://www.hello-algo.com/en/chapter_computational_complexity/summary/">2.5 &nbsp; Summary</a></li>
</ul>
@@ -2064,7 +2070,7 @@ aria-label="Footer"
<a
href="performance_evaluation/"
class="md-footer__link md-footer__link--next"
aria-label="Next: 2.1 Algorithm Efficiency Assessment"
aria-label="Next: 2.1 Algorithm efficiency assessment"
rel="next"
>
<div class="md-footer__title">
@@ -2072,7 +2078,7 @@ aria-label="Footer"
Next
</span>
<div class="md-ellipsis">
2.1 Algorithm Efficiency Assessment
2.1 Algorithm efficiency assessment
</div>
</div>
<div class="md-footer__button md-icon">
@@ -2182,13 +2188,13 @@ aria-label="Footer"
<a href="performance_evaluation/" class="md-footer__link md-footer__link--next" aria-label="Next: 2.1 Algorithm Efficiency Assessment">
<a href="performance_evaluation/" class="md-footer__link md-footer__link--next" aria-label="Next: 2.1 Algorithm efficiency assessment">
<div class="md-footer__title">
<span class="md-footer__direction">
Next
</span>
<div class="md-ellipsis">
2.1 Algorithm Efficiency Assessment
2.1 Algorithm efficiency assessment
</div>
</div>
<div class="md-footer__button md-icon">
@@ -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. &nbsp; For Loops
1. &nbsp; 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. &nbsp; While Loops
2. &nbsp; 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. &nbsp; Nested Loops
3. &nbsp; 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. &nbsp; Call Stack
1. &nbsp; 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. &nbsp; Tail Recursion
2. &nbsp; 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. &nbsp; Recursion Tree
3. &nbsp; 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. &nbsp; For Loops
1. &nbsp; 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. &nbsp; While Loops
2. &nbsp; 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. &nbsp; Nested Loops
3. &nbsp; 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. &nbsp; Call Stack
1. &nbsp; 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. &nbsp; Tail Recursion
2. &nbsp; 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. &nbsp; Recursion Tree
3. &nbsp; Recursion tree
</span>
</a>
@@ -2220,11 +2226,11 @@
<!-- Page content -->
<h1 id="22-iteration-and-recursion">2.2 &nbsp; Iteration and Recursion<a class="headerlink" href="#22-iteration-and-recursion" title="Permanent link">&para;</a></h1>
<h1 id="22-iteration-and-recursion">2.2 &nbsp; Iteration and recursion<a class="headerlink" href="#22-iteration-and-recursion" title="Permanent link">&para;</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 &nbsp; Iteration<a class="headerlink" href="#221-iteration" title="Permanent link">&para;</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. &nbsp; For Loops<a class="headerlink" href="#1-for-loops" title="Permanent link">&para;</a></h3>
<h3 id="1-for-loops">1. &nbsp; For loops<a class="headerlink" href="#1-for-loops" title="Permanent link">&para;</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">\(b1\)</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 &gt;</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 &nbsp; 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 &nbsp; 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. &nbsp; While Loops<a class="headerlink" href="#2-while-loops" title="Permanent link">&para;</a></h3>
<h3 id="2-while-loops">2. &nbsp; While loops<a class="headerlink" href="#2-while-loops" title="Permanent link">&para;</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 &gt;</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. &nbsp; Nested Loops<a class="headerlink" href="#3-nested-loops" title="Permanent link">&para;</a></h3>
<h3 id="3-nested-loops">3. &nbsp; Nested loops<a class="headerlink" href="#3-nested-loops" title="Permanent link">&para;</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 &gt;</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 &nbsp; 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 &nbsp; 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 &gt;</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 &nbsp; 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 &nbsp; 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. &nbsp; Call Stack<a class="headerlink" href="#1-call-stack" title="Permanent link">&para;</a></h3>
<h3 id="1-call-stack">1. &nbsp; Call stack<a class="headerlink" href="#1-call-stack" title="Permanent link">&para;</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 &nbsp; 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 &nbsp; 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. &nbsp; Tail Recursion<a class="headerlink" href="#2-tail-recursion" title="Permanent link">&para;</a></h3>
<h3 id="2-tail-recursion">2. &nbsp; Tail recursion<a class="headerlink" href="#2-tail-recursion" title="Permanent link">&para;</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 &nbsp; 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 &nbsp; 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. &nbsp; Recursion Tree<a class="headerlink" href="#3-recursion-tree" title="Permanent link">&para;</a></h3>
<h3 id="3-recursion-tree">3. &nbsp; Recursion tree<a class="headerlink" href="#3-recursion-tree" title="Permanent link">&para;</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 &gt;</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 &nbsp; 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 &nbsp; 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 &nbsp; Comparison<a class="headerlink" href="#223-comparison" title="Permanent link">&para;</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">
@@ -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">
@@ -26,7 +26,7 @@
<title>2.4 Space Complexity - Hello Algo</title>
<title>2.4 Space complexity - Hello Algo</title>
@@ -153,7 +153,7 @@
<div class="md-header__topic" data-md-component="header-topic">
<span class="md-ellipsis">
2.4 Space Complexity
2.4 Space complexity
</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>
@@ -682,7 +688,7 @@
<span class="md-ellipsis">
2.2 Iteration and Recursion
2.2 Iteration and recursion
</span>
@@ -703,7 +709,7 @@
<span class="md-ellipsis">
2.3 Time Complexity
2.3 Time complexity
</span>
@@ -733,7 +739,7 @@
<span class="md-ellipsis">
2.4 Space Complexity
2.4 Space complexity
</span>
@@ -744,7 +750,7 @@
<span class="md-ellipsis">
2.4 Space Complexity
2.4 Space complexity
</span>
@@ -768,7 +774,7 @@
<li class="md-nav__item">
<a href="#241-space-related-to-algorithms" class="md-nav__link">
<span class="md-ellipsis">
2.4.1 &nbsp; Space Related to Algorithms
2.4.1 &nbsp; Space related to algorithms
</span>
</a>
@@ -777,7 +783,7 @@
<li class="md-nav__item">
<a href="#242-calculation-method" class="md-nav__link">
<span class="md-ellipsis">
2.4.2 &nbsp; Calculation Method
2.4.2 &nbsp; Calculation method
</span>
</a>
@@ -786,17 +792,17 @@
<li class="md-nav__item">
<a href="#243-common-types" class="md-nav__link">
<span class="md-ellipsis">
2.4.3 &nbsp; Common Types
2.4.3 &nbsp; Common types
</span>
</a>
<nav class="md-nav" aria-label="2.4.3   Common Types">
<nav class="md-nav" aria-label="2.4.3   Common types">
<ul class="md-nav__list">
<li class="md-nav__item">
<a href="#1-constant-order-o1" class="md-nav__link">
<span class="md-ellipsis">
1. &nbsp; Constant Order
1. &nbsp; Constant order
</span>
</a>
@@ -805,7 +811,7 @@
<li class="md-nav__item">
<a href="#2-linear-order-on" class="md-nav__link">
<span class="md-ellipsis">
2. &nbsp; Linear Order
2. &nbsp; Linear order
</span>
</a>
@@ -814,7 +820,7 @@
<li class="md-nav__item">
<a href="#3-quadratic-order-on2" class="md-nav__link">
<span class="md-ellipsis">
3. &nbsp; Quadratic Order
3. &nbsp; Quadratic order
</span>
</a>
@@ -823,7 +829,7 @@
<li class="md-nav__item">
<a href="#4-exponential-order-o2n" class="md-nav__link">
<span class="md-ellipsis">
4. &nbsp; Exponential Order
4. &nbsp; Exponential order
</span>
</a>
@@ -832,7 +838,7 @@
<li class="md-nav__item">
<a href="#5-logarithmic-order-olog-n" class="md-nav__link">
<span class="md-ellipsis">
5. &nbsp; Logarithmic Order
5. &nbsp; Logarithmic order
</span>
</a>
@@ -846,7 +852,7 @@
<li class="md-nav__item">
<a href="#244-balancing-time-and-space" class="md-nav__link">
<span class="md-ellipsis">
2.4.4 &nbsp; Balancing Time and Space
2.4.4 &nbsp; Balancing time and space
</span>
</a>
@@ -932,7 +938,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>
@@ -948,7 +954,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>
@@ -965,7 +971,7 @@
<span class="md-ellipsis">
3.1 Classification of Data Structures
3.1 Classification of data structures
</span>
@@ -986,7 +992,7 @@
<span class="md-ellipsis">
3.2 Fundamental Data Types
3.2 Fundamental data types
</span>
@@ -1007,7 +1013,7 @@
<span class="md-ellipsis">
3.3 Number Encoding *
3.3 Number encoding *
</span>
@@ -1028,7 +1034,7 @@
<span class="md-ellipsis">
3.4 Character Encoding *
3.4 Character encoding *
</span>
@@ -1109,7 +1115,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>
@@ -1125,7 +1131,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>
@@ -1163,7 +1169,7 @@
<span class="md-ellipsis">
4.2 Linked List
4.2 Linked list
</span>
@@ -1205,7 +1211,7 @@
<span class="md-ellipsis">
4.4 Memory and Cache
4.4 Memory and cache
</span>
@@ -1284,7 +1290,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>
@@ -1300,7 +1306,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>
@@ -1359,7 +1365,7 @@
<span class="md-ellipsis">
5.3 Double-ended Queue
5.3 Double-ended queue
</span>
@@ -1438,7 +1444,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>
@@ -1454,7 +1460,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>
@@ -1471,7 +1477,7 @@
<span class="md-ellipsis">
6.1 Hash Table
6.1 Hash table
</span>
@@ -1492,7 +1498,7 @@
<span class="md-ellipsis">
6.2 Hash Collision
6.2 Hash collision
</span>
@@ -1513,7 +1519,7 @@
<span class="md-ellipsis">
6.3 Hash Algorithm
6.3 Hash algorithm
</span>
@@ -2088,7 +2094,7 @@
<li class="md-nav__item">
<a href="#241-space-related-to-algorithms" class="md-nav__link">
<span class="md-ellipsis">
2.4.1 &nbsp; Space Related to Algorithms
2.4.1 &nbsp; Space related to algorithms
</span>
</a>
@@ -2097,7 +2103,7 @@
<li class="md-nav__item">
<a href="#242-calculation-method" class="md-nav__link">
<span class="md-ellipsis">
2.4.2 &nbsp; Calculation Method
2.4.2 &nbsp; Calculation method
</span>
</a>
@@ -2106,17 +2112,17 @@
<li class="md-nav__item">
<a href="#243-common-types" class="md-nav__link">
<span class="md-ellipsis">
2.4.3 &nbsp; Common Types
2.4.3 &nbsp; Common types
</span>
</a>
<nav class="md-nav" aria-label="2.4.3   Common Types">
<nav class="md-nav" aria-label="2.4.3   Common types">
<ul class="md-nav__list">
<li class="md-nav__item">
<a href="#1-constant-order-o1" class="md-nav__link">
<span class="md-ellipsis">
1. &nbsp; Constant Order
1. &nbsp; Constant order
</span>
</a>
@@ -2125,7 +2131,7 @@
<li class="md-nav__item">
<a href="#2-linear-order-on" class="md-nav__link">
<span class="md-ellipsis">
2. &nbsp; Linear Order
2. &nbsp; Linear order
</span>
</a>
@@ -2134,7 +2140,7 @@
<li class="md-nav__item">
<a href="#3-quadratic-order-on2" class="md-nav__link">
<span class="md-ellipsis">
3. &nbsp; Quadratic Order
3. &nbsp; Quadratic order
</span>
</a>
@@ -2143,7 +2149,7 @@
<li class="md-nav__item">
<a href="#4-exponential-order-o2n" class="md-nav__link">
<span class="md-ellipsis">
4. &nbsp; Exponential Order
4. &nbsp; Exponential order
</span>
</a>
@@ -2152,7 +2158,7 @@
<li class="md-nav__item">
<a href="#5-logarithmic-order-olog-n" class="md-nav__link">
<span class="md-ellipsis">
5. &nbsp; Logarithmic Order
5. &nbsp; Logarithmic order
</span>
</a>
@@ -2166,7 +2172,7 @@
<li class="md-nav__item">
<a href="#244-balancing-time-and-space" class="md-nav__link">
<span class="md-ellipsis">
2.4.4 &nbsp; Balancing Time and Space
2.4.4 &nbsp; Balancing time and space
</span>
</a>
@@ -2208,38 +2214,38 @@
<!-- Page content -->
<h1 id="24-space-complexity">2.4 &nbsp; Space Complexity<a class="headerlink" href="#24-space-complexity" title="Permanent link">&para;</a></h1>
<h1 id="24-space-complexity">2.4 &nbsp; Space complexity<a class="headerlink" href="#24-space-complexity" title="Permanent link">&para;</a></h1>
<p>"Space complexity" is used to measure the growth trend of the memory space occupied by an algorithm as the amount of data increases. This concept is very similar to time complexity, except that "running time" is replaced with "occupied memory space".</p>
<h2 id="241-space-related-to-algorithms">2.4.1 &nbsp; Space Related to Algorithms<a class="headerlink" href="#241-space-related-to-algorithms" title="Permanent link">&para;</a></h2>
<h2 id="241-space-related-to-algorithms">2.4.1 &nbsp; Space related to algorithms<a class="headerlink" href="#241-space-related-to-algorithms" title="Permanent link">&para;</a></h2>
<p>The memory space used by an algorithm during its execution mainly includes the following types.</p>
<ul>
<li><strong>Input Space</strong>: Used to store the input data of the algorithm.</li>
<li><strong>Temporary Space</strong>: Used to store variables, objects, function contexts, and other data during the algorithm's execution.</li>
<li><strong>Output Space</strong>: Used to store the output data of the algorithm.</li>
<li><strong>Input space</strong>: Used to store the input data of the algorithm.</li>
<li><strong>Temporary space</strong>: Used to store variables, objects, function contexts, and other data during the algorithm's execution.</li>
<li><strong>Output space</strong>: Used to store the output data of the algorithm.</li>
</ul>
<p>Generally, the scope of space complexity statistics includes both "Temporary Space" and "Output Space".</p>
<p>Temporary space can be further divided into three parts.</p>
<ul>
<li><strong>Temporary Data</strong>: Used to save various constants, variables, objects, etc., during the algorithm's execution.</li>
<li><strong>Stack Frame Space</strong>: Used to save the context data of the called function. The system creates a stack frame at the top of the stack each time a function is called, and the stack frame space is released after the function returns.</li>
<li><strong>Instruction Space</strong>: Used to store compiled program instructions, which are usually negligible in actual statistics.</li>
<li><strong>Temporary data</strong>: Used to save various constants, variables, objects, etc., during the algorithm's execution.</li>
<li><strong>Stack frame space</strong>: Used to save the context data of the called function. The system creates a stack frame at the top of the stack each time a function is called, and the stack frame space is released after the function returns.</li>
<li><strong>Instruction space</strong>: Used to store compiled program instructions, which are usually negligible in actual statistics.</li>
</ul>
<p>When analyzing the space complexity of a program, <strong>we typically count the Temporary Data, Stack Frame Space, and Output Data</strong>, as shown in the Figure 2-15 .</p>
<p><a class="glightbox" href="../space_complexity.assets/space_types.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Space Types Used in Algorithms" class="animation-figure" src="../space_complexity.assets/space_types.png" /></a></p>
<p align="center"> Figure 2-15 &nbsp; Space Types Used in Algorithms </p>
<p><a class="glightbox" href="../space_complexity.assets/space_types.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Space types used in algorithms" class="animation-figure" src="../space_complexity.assets/space_types.png" /></a></p>
<p align="center"> Figure 2-15 &nbsp; Space types used in algorithms </p>
<p>The relevant code is as follows:</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">Zig</label></div>
<div class="tabbed-content">
<div class="tabbed-block">
<div class="highlight"><pre><span></span><code><a id="__codelineno-0-1" name="__codelineno-0-1" href="#__codelineno-0-1"></a><span class="k">class</span> <span class="nc">Node</span><span class="p">:</span>
<a id="__codelineno-0-2" name="__codelineno-0-2" href="#__codelineno-0-2"></a><span class="w"> </span><span class="sd">&quot;&quot;&quot;Classes&quot;&quot;&quot;</span><span class="s2">&quot;</span>
<a id="__codelineno-0-2" name="__codelineno-0-2" href="#__codelineno-0-2"></a><span class="w"> </span><span class="sd">&quot;&quot;&quot;Classes&quot;&quot;&quot;</span>
<a id="__codelineno-0-3" name="__codelineno-0-3" href="#__codelineno-0-3"></a> <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">:</span> <span class="nb">int</span><span class="p">):</span>
<a id="__codelineno-0-4" name="__codelineno-0-4" href="#__codelineno-0-4"></a> <span class="bp">self</span><span class="o">.</span><span class="n">val</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="n">x</span> <span class="c1"># node value</span>
<a id="__codelineno-0-5" name="__codelineno-0-5" href="#__codelineno-0-5"></a> <span class="bp">self</span><span class="o">.</span><span class="n">next</span><span class="p">:</span> <span class="n">Node</span> <span class="o">|</span> <span class="kc">None</span> <span class="o">=</span> <span class="kc">None</span> <span class="c1"># reference to the next node</span>
<a id="__codelineno-0-6" name="__codelineno-0-6" href="#__codelineno-0-6"></a>
<a id="__codelineno-0-7" name="__codelineno-0-7" href="#__codelineno-0-7"></a><span class="k">def</span> <span class="nf">function</span><span class="p">()</span> <span class="o">-&gt;</span> <span class="nb">int</span><span class="p">:</span>
<a id="__codelineno-0-8" name="__codelineno-0-8" href="#__codelineno-0-8"></a><span class="w"> </span><span class="sd">&quot;&quot;&quot;&quot;Functions&quot;&quot;&quot;</span><span class="s2">&quot;&quot;</span>
<a id="__codelineno-0-8" name="__codelineno-0-8" href="#__codelineno-0-8"></a><span class="w"> </span><span class="sd">&quot;&quot;&quot;Functions&quot;&quot;&quot;</span>
<a id="__codelineno-0-9" name="__codelineno-0-9" href="#__codelineno-0-9"></a> <span class="c1"># Perform certain operations...</span>
<a id="__codelineno-0-10" name="__codelineno-0-10" href="#__codelineno-0-10"></a> <span class="k">return</span> <span class="mi">0</span>
<a id="__codelineno-0-11" name="__codelineno-0-11" href="#__codelineno-0-11"></a>
@@ -2458,7 +2464,7 @@
<a id="__codelineno-9-7" name="__codelineno-9-7" href="#__codelineno-9-7"></a><span class="w"> </span><span class="n">next</span>: <span class="nb">Option</span><span class="o">&lt;</span><span class="n">Rc</span><span class="o">&lt;</span><span class="n">RefCell</span><span class="o">&lt;</span><span class="n">Node</span><span class="o">&gt;&gt;&gt;</span><span class="p">,</span>
<a id="__codelineno-9-8" name="__codelineno-9-8" href="#__codelineno-9-8"></a><span class="p">}</span>
<a id="__codelineno-9-9" name="__codelineno-9-9" href="#__codelineno-9-9"></a>
<a id="__codelineno-9-10" name="__codelineno-9-10" href="#__codelineno-9-10"></a><span class="cm">/* Creating a Node structure */</span>
<a id="__codelineno-9-10" name="__codelineno-9-10" href="#__codelineno-9-10"></a><span class="cm">/* Constructor */</span>
<a id="__codelineno-9-11" name="__codelineno-9-11" href="#__codelineno-9-11"></a><span class="k">impl</span><span class="w"> </span><span class="n">Node</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-9-12" name="__codelineno-9-12" href="#__codelineno-9-12"></a><span class="w"> </span><span class="k">fn</span> <span class="nf">new</span><span class="p">(</span><span class="n">val</span>: <span class="kt">i32</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nc">Self</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-9-13" name="__codelineno-9-13" href="#__codelineno-9-13"></a><span class="w"> </span><span class="bp">Self</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">val</span>: <span class="nc">val</span><span class="p">,</span><span class="w"> </span><span class="n">next</span>: <span class="nb">None</span> <span class="p">}</span>
@@ -2505,7 +2511,7 @@
</div>
</div>
</div>
<h2 id="242-calculation-method">2.4.2 &nbsp; Calculation Method<a class="headerlink" href="#242-calculation-method" title="Permanent link">&para;</a></h2>
<h2 id="242-calculation-method">2.4.2 &nbsp; Calculation method<a class="headerlink" href="#242-calculation-method" title="Permanent link">&para;</a></h2>
<p>The method for calculating space complexity is roughly similar to that of time complexity, with the only change being the shift of the statistical object from "number of operations" to "size of used space".</p>
<p>However, unlike time complexity, <strong>we usually only focus on the worst-case space complexity</strong>. This is because memory space is a hard requirement, and we must ensure that there is enough memory space reserved under all input data.</p>
<p>Consider the following code, the term "worst-case" in worst-case space complexity has two meanings.</p>
@@ -2605,10 +2611,10 @@
</div>
<div class="tabbed-block">
<div class="highlight"><pre><span></span><code><a id="__codelineno-22-1" name="__codelineno-22-1" href="#__codelineno-22-1"></a><span class="k">fn</span> <span class="nf">algorithm</span><span class="p">(</span><span class="n">n</span>: <span class="kt">i32</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-22-2" name="__codelineno-22-2" href="#__codelineno-22-2"></a><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="c1">// O(1)</span>
<a id="__codelineno-22-3" name="__codelineno-22-3" href="#__codelineno-22-3"></a><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="mi">10000</span><span class="p">];</span><span class="w"> </span><span class="c1">// O(1)</span>
<a id="__codelineno-22-2" name="__codelineno-22-2" href="#__codelineno-22-2"></a><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="c1">// O(1)</span>
<a id="__codelineno-22-3" name="__codelineno-22-3" href="#__codelineno-22-3"></a><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="mi">10000</span><span class="p">];</span><span class="w"> </span><span class="c1">// O(1)</span>
<a id="__codelineno-22-4" name="__codelineno-22-4" href="#__codelineno-22-4"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="o">&gt;</span><span class="w"> </span><span class="mi">10</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-22-5" name="__codelineno-22-5" href="#__codelineno-22-5"></a><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">nums</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="fm">vec!</span><span class="p">[</span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="kt">usize</span><span class="p">];</span><span class="w"> </span><span class="c1">// O(n)</span>
<a id="__codelineno-22-5" name="__codelineno-22-5" href="#__codelineno-22-5"></a><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">nums</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="fm">vec!</span><span class="p">[</span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="kt">usize</span><span class="p">];</span><span class="w"> </span><span class="c1">// O(n)</span>
<a id="__codelineno-22-6" name="__codelineno-22-6" href="#__codelineno-22-6"></a><span class="w"> </span><span class="p">}</span>
<a id="__codelineno-22-7" name="__codelineno-22-7" href="#__codelineno-22-7"></a><span class="p">}</span>
</code></pre></div>
@@ -2641,12 +2647,12 @@
<a id="__codelineno-26-3" name="__codelineno-26-3" href="#__codelineno-26-3"></a> <span class="k">return</span> <span class="mi">0</span>
<a id="__codelineno-26-4" name="__codelineno-26-4" href="#__codelineno-26-4"></a>
<a id="__codelineno-26-5" name="__codelineno-26-5" href="#__codelineno-26-5"></a><span class="k">def</span> <span class="nf">loop</span><span class="p">(</span><span class="n">n</span><span class="p">:</span> <span class="nb">int</span><span class="p">):</span>
<a id="__codelineno-26-6" name="__codelineno-26-6" href="#__codelineno-26-6"></a><span class="w"> </span><span class="sd">&quot;&quot;&quot;Loop O(1)&quot;&quot;&quot;</span><span class="s2">&quot;&quot;</span>
<a id="__codelineno-26-6" name="__codelineno-26-6" href="#__codelineno-26-6"></a><span class="w"> </span><span class="sd">&quot;&quot;&quot;Loop O(1)&quot;&quot;&quot;</span>
<a id="__codelineno-26-7" name="__codelineno-26-7" href="#__codelineno-26-7"></a> <span class="k">for</span> <span class="n">_</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
<a id="__codelineno-26-8" name="__codelineno-26-8" href="#__codelineno-26-8"></a> <span class="n">function</span><span class="p">()</span>
<a id="__codelineno-26-9" name="__codelineno-26-9" href="#__codelineno-26-9"></a>
<a id="__codelineno-26-10" name="__codelineno-26-10" href="#__codelineno-26-10"></a><span class="k">def</span> <span class="nf">recur</span><span class="p">(</span><span class="n">n</span><span class="p">:</span> <span class="nb">int</span><span class="p">):</span>
<a id="__codelineno-26-11" name="__codelineno-26-11" href="#__codelineno-26-11"></a><span class="w"> </span><span class="sd">&quot;&quot;&quot;Recursion O(n)&quot;&quot;&quot;</span><span class="s2">&quot;&quot;</span>
<a id="__codelineno-26-11" name="__codelineno-26-11" href="#__codelineno-26-11"></a><span class="w"> </span><span class="sd">&quot;&quot;&quot;Recursion O(n)&quot;&quot;&quot;</span>
<a id="__codelineno-26-12" name="__codelineno-26-12" href="#__codelineno-26-12"></a> <span class="k">if</span> <span class="n">n</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
<a id="__codelineno-26-13" name="__codelineno-26-13" href="#__codelineno-26-13"></a> <span class="k">return</span>
<a id="__codelineno-26-14" name="__codelineno-26-14" href="#__codelineno-26-14"></a> <span class="k">return</span> <span class="n">recur</span><span class="p">(</span><span class="n">n</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span>
@@ -2858,7 +2864,7 @@
<li>The <code>loop()</code> function calls <code>function()</code> <span class="arithmatex">\(n\)</span> times in a loop, where each iteration's <code>function()</code> returns and releases its stack frame space, so the space complexity remains <span class="arithmatex">\(O(1)\)</span>.</li>
<li>The recursive function <code>recur()</code> will have <span class="arithmatex">\(n\)</span> instances of unreturned <code>recur()</code> existing simultaneously during its execution, thus occupying <span class="arithmatex">\(O(n)\)</span> stack frame space.</li>
</ul>
<h2 id="243-common-types">2.4.3 &nbsp; Common Types<a class="headerlink" href="#243-common-types" title="Permanent link">&para;</a></h2>
<h2 id="243-common-types">2.4.3 &nbsp; Common types<a class="headerlink" href="#243-common-types" title="Permanent link">&para;</a></h2>
<p>Let the size of the input data be <span class="arithmatex">\(n\)</span>, the following chart displays common types of space complexities (arranged from low to high).</p>
<div class="arithmatex">\[
\begin{aligned}
@@ -2866,10 +2872,10 @@ O(1) &lt; O(\log n) &lt; O(n) &lt; O(n^2) &lt; O(2^n) \newline
\text{Constant Order} &lt; \text{Logarithmic Order} &lt; \text{Linear Order} &lt; \text{Quadratic Order} &lt; \text{Exponential Order}
\end{aligned}
\]</div>
<p><a class="glightbox" href="../space_complexity.assets/space_complexity_common_types.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Common Types of Space Complexity" class="animation-figure" src="../space_complexity.assets/space_complexity_common_types.png" /></a></p>
<p align="center"> Figure 2-16 &nbsp; Common Types of Space Complexity </p>
<p><a class="glightbox" href="../space_complexity.assets/space_complexity_common_types.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Common types of space complexity" class="animation-figure" src="../space_complexity.assets/space_complexity_common_types.png" /></a></p>
<p align="center"> Figure 2-16 &nbsp; Common types of space complexity </p>
<h3 id="1-constant-order-o1">1. &nbsp; Constant Order <span class="arithmatex">\(O(1)\)</span><a class="headerlink" href="#1-constant-order-o1" title="Permanent link">&para;</a></h3>
<h3 id="1-constant-order-o1">1. &nbsp; Constant order <span class="arithmatex">\(O(1)\)</span><a class="headerlink" href="#1-constant-order-o1" title="Permanent link">&para;</a></h3>
<p>Constant order is common in constants, variables, objects that are independent of the size of input data <span class="arithmatex">\(n\)</span>.</p>
<p>Note that memory occupied by initializing variables or calling functions in a loop, which is released upon entering the next cycle, does not accumulate over space, thus the space complexity remains <span class="arithmatex">\(O(1)\)</span>:</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>
@@ -3237,7 +3243,7 @@ O(1) &lt; O(\log n) &lt; O(n) &lt; O(n^2) &lt; O(2^n) \newline
<p><div style="height: 549px; width: 100%;"><iframe class="pythontutor-iframe" src="https://pythontutor.com/iframe-embed.html#code=class%20ListNode%3A%0A%20%20%20%20%22%22%22%E9%93%BE%E8%A1%A8%E8%8A%82%E7%82%B9%E7%B1%BB%22%22%22%0A%20%20%20%20def%20__init__%28self,%20val%3A%20int%29%3A%0A%20%20%20%20%20%20%20%20self.val%3A%20int%20%3D%20val%20%20%23%20%E8%8A%82%E7%82%B9%E5%80%BC%0A%20%20%20%20%20%20%20%20self.next%3A%20ListNode%20%7C%20None%20%3D%20None%20%20%23%20%E5%90%8E%E7%BB%A7%E8%8A%82%E7%82%B9%E5%BC%95%E7%94%A8%0A%0Adef%20function%28%29%20-%3E%20int%3A%0A%20%20%20%20%22%22%22%E5%87%BD%E6%95%B0%22%22%22%0A%20%20%20%20%23%20%E6%89%A7%E8%A1%8C%E6%9F%90%E4%BA%9B%E6%93%8D%E4%BD%9C%0A%20%20%20%20return%200%0A%0Adef%20constant%28n%3A%20int%29%3A%0A%20%20%20%20%22%22%22%E5%B8%B8%E6%95%B0%E9%98%B6%22%22%22%0A%20%20%20%20%23%20%E5%B8%B8%E9%87%8F%E3%80%81%E5%8F%98%E9%87%8F%E3%80%81%E5%AF%B9%E8%B1%A1%E5%8D%A0%E7%94%A8%20O%281%29%20%E7%A9%BA%E9%97%B4%0A%20%20%20%20a%20%3D%200%0A%20%20%20%20nums%20%3D%20%5B0%5D%20*%2010%0A%20%20%20%20node%20%3D%20ListNode%280%29%0A%20%20%20%20%23%20%E5%BE%AA%E7%8E%AF%E4%B8%AD%E7%9A%84%E5%8F%98%E9%87%8F%E5%8D%A0%E7%94%A8%20O%281%29%20%E7%A9%BA%E9%97%B4%0A%20%20%20%20for%20_%20in%20range%28n%29%3A%0A%20%20%20%20%20%20%20%20c%20%3D%200%0A%20%20%20%20%23%20%E5%BE%AA%E7%8E%AF%E4%B8%AD%E7%9A%84%E5%87%BD%E6%95%B0%E5%8D%A0%E7%94%A8%20O%281%29%20%E7%A9%BA%E9%97%B4%0A%20%20%20%20for%20_%20in%20range%28n%29%3A%0A%20%20%20%20%20%20%20%20function%28%29%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%20print%28%22%E8%BE%93%E5%85%A5%E6%95%B0%E6%8D%AE%E5%A4%A7%E5%B0%8F%20n%20%3D%22,%20n%29%0A%0A%20%20%20%20%23%20%E5%B8%B8%E6%95%B0%E9%98%B6%0A%20%20%20%20constant%28n%29&codeDivHeight=472&codeDivWidth=350&cumulative=false&curInstr=6&heapPrimitives=nevernest&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false"> </iframe></div>
<div style="margin-top: 5px;"><a href="https://pythontutor.com/iframe-embed.html#code=class%20ListNode%3A%0A%20%20%20%20%22%22%22%E9%93%BE%E8%A1%A8%E8%8A%82%E7%82%B9%E7%B1%BB%22%22%22%0A%20%20%20%20def%20__init__%28self,%20val%3A%20int%29%3A%0A%20%20%20%20%20%20%20%20self.val%3A%20int%20%3D%20val%20%20%23%20%E8%8A%82%E7%82%B9%E5%80%BC%0A%20%20%20%20%20%20%20%20self.next%3A%20ListNode%20%7C%20None%20%3D%20None%20%20%23%20%E5%90%8E%E7%BB%A7%E8%8A%82%E7%82%B9%E5%BC%95%E7%94%A8%0A%0Adef%20function%28%29%20-%3E%20int%3A%0A%20%20%20%20%22%22%22%E5%87%BD%E6%95%B0%22%22%22%0A%20%20%20%20%23%20%E6%89%A7%E8%A1%8C%E6%9F%90%E4%BA%9B%E6%93%8D%E4%BD%9C%0A%20%20%20%20return%200%0A%0Adef%20constant%28n%3A%20int%29%3A%0A%20%20%20%20%22%22%22%E5%B8%B8%E6%95%B0%E9%98%B6%22%22%22%0A%20%20%20%20%23%20%E5%B8%B8%E9%87%8F%E3%80%81%E5%8F%98%E9%87%8F%E3%80%81%E5%AF%B9%E8%B1%A1%E5%8D%A0%E7%94%A8%20O%281%29%20%E7%A9%BA%E9%97%B4%0A%20%20%20%20a%20%3D%200%0A%20%20%20%20nums%20%3D%20%5B0%5D%20*%2010%0A%20%20%20%20node%20%3D%20ListNode%280%29%0A%20%20%20%20%23%20%E5%BE%AA%E7%8E%AF%E4%B8%AD%E7%9A%84%E5%8F%98%E9%87%8F%E5%8D%A0%E7%94%A8%20O%281%29%20%E7%A9%BA%E9%97%B4%0A%20%20%20%20for%20_%20in%20range%28n%29%3A%0A%20%20%20%20%20%20%20%20c%20%3D%200%0A%20%20%20%20%23%20%E5%BE%AA%E7%8E%AF%E4%B8%AD%E7%9A%84%E5%87%BD%E6%95%B0%E5%8D%A0%E7%94%A8%20O%281%29%20%E7%A9%BA%E9%97%B4%0A%20%20%20%20for%20_%20in%20range%28n%29%3A%0A%20%20%20%20%20%20%20%20function%28%29%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%20print%28%22%E8%BE%93%E5%85%A5%E6%95%B0%E6%8D%AE%E5%A4%A7%E5%B0%8F%20n%20%3D%22,%20n%29%0A%0A%20%20%20%20%23%20%E5%B8%B8%E6%95%B0%E9%98%B6%0A%20%20%20%20constant%28n%29&codeDivHeight=800&codeDivWidth=600&cumulative=false&curInstr=6&heapPrimitives=nevernest&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false" target="_blank" rel="noopener noreferrer">Full Screen &gt;</a></div></p>
</details>
<h3 id="2-linear-order-on">2. &nbsp; Linear Order <span class="arithmatex">\(O(n)\)</span><a class="headerlink" href="#2-linear-order-on" title="Permanent link">&para;</a></h3>
<h3 id="2-linear-order-on">2. &nbsp; Linear order <span class="arithmatex">\(O(n)\)</span><a class="headerlink" href="#2-linear-order-on" title="Permanent link">&para;</a></h3>
<p>Linear order is common in arrays, linked lists, stacks, queues, etc., where the number of elements is proportional to <span class="arithmatex">\(n\)</span>:</p>
<div class="tabbed-set tabbed-alternate" data-tabs="5:14"><input checked="checked" id="__tabbed_5_1" name="__tabbed_5" type="radio" /><input id="__tabbed_5_2" name="__tabbed_5" type="radio" /><input id="__tabbed_5_3" name="__tabbed_5" type="radio" /><input id="__tabbed_5_4" name="__tabbed_5" type="radio" /><input id="__tabbed_5_5" name="__tabbed_5" type="radio" /><input id="__tabbed_5_6" name="__tabbed_5" type="radio" /><input id="__tabbed_5_7" name="__tabbed_5" type="radio" /><input id="__tabbed_5_8" name="__tabbed_5" type="radio" /><input id="__tabbed_5_9" name="__tabbed_5" type="radio" /><input id="__tabbed_5_10" name="__tabbed_5" type="radio" /><input id="__tabbed_5_11" name="__tabbed_5" type="radio" /><input id="__tabbed_5_12" name="__tabbed_5" type="radio" /><input id="__tabbed_5_13" name="__tabbed_5" type="radio" /><input id="__tabbed_5_14" name="__tabbed_5" type="radio" /><div class="tabbed-labels"><label for="__tabbed_5_1">Python</label><label for="__tabbed_5_2">C++</label><label for="__tabbed_5_3">Java</label><label for="__tabbed_5_4">C#</label><label for="__tabbed_5_5">Go</label><label for="__tabbed_5_6">Swift</label><label for="__tabbed_5_7">JS</label><label for="__tabbed_5_8">TS</label><label for="__tabbed_5_9">Dart</label><label for="__tabbed_5_10">Rust</label><label for="__tabbed_5_11">C</label><label for="__tabbed_5_12">Kotlin</label><label for="__tabbed_5_13">Ruby</label><label for="__tabbed_5_14">Zig</label></div>
<div class="tabbed-content">
@@ -3662,10 +3668,10 @@ O(1) &lt; O(\log n) &lt; O(n) &lt; O(n^2) &lt; O(2^n) \newline
<p><div style="height: 441px; width: 100%;"><iframe class="pythontutor-iframe" src="https://pythontutor.com/iframe-embed.html#code=def%20linear_recur%28n%3A%20int%29%3A%0A%20%20%20%20%22%22%22%E7%BA%BF%E6%80%A7%E9%98%B6%EF%BC%88%E9%80%92%E5%BD%92%E5%AE%9E%E7%8E%B0%EF%BC%89%22%22%22%0A%20%20%20%20print%28%22%E9%80%92%E5%BD%92%20n%20%3D%22,%20n%29%0A%20%20%20%20if%20n%20%3D%3D%201%3A%0A%20%20%20%20%20%20%20%20return%0A%20%20%20%20linear_recur%28n%20-%201%29%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%20print%28%22%E8%BE%93%E5%85%A5%E6%95%B0%E6%8D%AE%E5%A4%A7%E5%B0%8F%20n%20%3D%22,%20n%29%0A%0A%20%20%20%20%23%20%E7%BA%BF%E6%80%A7%E9%98%B6%0A%20%20%20%20linear_recur%28n%29&codeDivHeight=472&codeDivWidth=350&cumulative=false&curInstr=25&heapPrimitives=nevernest&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false"> </iframe></div>
<div style="margin-top: 5px;"><a href="https://pythontutor.com/iframe-embed.html#code=def%20linear_recur%28n%3A%20int%29%3A%0A%20%20%20%20%22%22%22%E7%BA%BF%E6%80%A7%E9%98%B6%EF%BC%88%E9%80%92%E5%BD%92%E5%AE%9E%E7%8E%B0%EF%BC%89%22%22%22%0A%20%20%20%20print%28%22%E9%80%92%E5%BD%92%20n%20%3D%22,%20n%29%0A%20%20%20%20if%20n%20%3D%3D%201%3A%0A%20%20%20%20%20%20%20%20return%0A%20%20%20%20linear_recur%28n%20-%201%29%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%20print%28%22%E8%BE%93%E5%85%A5%E6%95%B0%E6%8D%AE%E5%A4%A7%E5%B0%8F%20n%20%3D%22,%20n%29%0A%0A%20%20%20%20%23%20%E7%BA%BF%E6%80%A7%E9%98%B6%0A%20%20%20%20linear_recur%28n%29&codeDivHeight=800&codeDivWidth=600&cumulative=false&curInstr=25&heapPrimitives=nevernest&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false" target="_blank" rel="noopener noreferrer">Full Screen &gt;</a></div></p>
</details>
<p><a class="glightbox" href="../space_complexity.assets/space_complexity_recursive_linear.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Recursive Function Generating Linear Order Space Complexity" class="animation-figure" src="../space_complexity.assets/space_complexity_recursive_linear.png" /></a></p>
<p align="center"> Figure 2-17 &nbsp; Recursive Function Generating Linear Order Space Complexity </p>
<p><a class="glightbox" href="../space_complexity.assets/space_complexity_recursive_linear.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Recursive function generating linear order space complexity" class="animation-figure" src="../space_complexity.assets/space_complexity_recursive_linear.png" /></a></p>
<p align="center"> Figure 2-17 &nbsp; Recursive function generating linear order space complexity </p>
<h3 id="3-quadratic-order-on2">3. &nbsp; Quadratic Order <span class="arithmatex">\(O(n^2)\)</span><a class="headerlink" href="#3-quadratic-order-on2" title="Permanent link">&para;</a></h3>
<h3 id="3-quadratic-order-on2">3. &nbsp; Quadratic order <span class="arithmatex">\(O(n^2)\)</span><a class="headerlink" href="#3-quadratic-order-on2" title="Permanent link">&para;</a></h3>
<p>Quadratic order is common in matrices and graphs, where the number of elements is quadratic to <span class="arithmatex">\(n\)</span>:</p>
<div class="tabbed-set tabbed-alternate" data-tabs="7:14"><input checked="checked" id="__tabbed_7_1" name="__tabbed_7" type="radio" /><input id="__tabbed_7_2" name="__tabbed_7" type="radio" /><input id="__tabbed_7_3" name="__tabbed_7" type="radio" /><input id="__tabbed_7_4" name="__tabbed_7" type="radio" /><input id="__tabbed_7_5" name="__tabbed_7" type="radio" /><input id="__tabbed_7_6" name="__tabbed_7" type="radio" /><input id="__tabbed_7_7" name="__tabbed_7" type="radio" /><input id="__tabbed_7_8" name="__tabbed_7" type="radio" /><input id="__tabbed_7_9" name="__tabbed_7" type="radio" /><input id="__tabbed_7_10" name="__tabbed_7" type="radio" /><input id="__tabbed_7_11" name="__tabbed_7" type="radio" /><input id="__tabbed_7_12" name="__tabbed_7" type="radio" /><input id="__tabbed_7_13" name="__tabbed_7" type="radio" /><input id="__tabbed_7_14" name="__tabbed_7" type="radio" /><div class="tabbed-labels"><label for="__tabbed_7_1">Python</label><label for="__tabbed_7_2">C++</label><label for="__tabbed_7_3">Java</label><label for="__tabbed_7_4">C#</label><label for="__tabbed_7_5">Go</label><label for="__tabbed_7_6">Swift</label><label for="__tabbed_7_7">JS</label><label for="__tabbed_7_8">TS</label><label for="__tabbed_7_9">Dart</label><label for="__tabbed_7_10">Rust</label><label for="__tabbed_7_11">C</label><label for="__tabbed_7_12">Kotlin</label><label for="__tabbed_7_13">Ruby</label><label for="__tabbed_7_14">Zig</label></div>
<div class="tabbed-content">
@@ -4057,10 +4063,10 @@ O(1) &lt; O(\log n) &lt; O(n) &lt; O(n^2) &lt; O(2^n) \newline
<p><div style="height: 459px; width: 100%;"><iframe class="pythontutor-iframe" src="https://pythontutor.com/iframe-embed.html#code=def%20quadratic_recur%28n%3A%20int%29%20-%3E%20int%3A%0A%20%20%20%20%22%22%22%E5%B9%B3%E6%96%B9%E9%98%B6%EF%BC%88%E9%80%92%E5%BD%92%E5%AE%9E%E7%8E%B0%EF%BC%89%22%22%22%0A%20%20%20%20if%20n%20%3C%3D%200%3A%0A%20%20%20%20%20%20%20%20return%200%0A%20%20%20%20%23%20%E6%95%B0%E7%BB%84%20nums%20%E9%95%BF%E5%BA%A6%E4%B8%BA%20n,%20n-1,%20...,%202,%201%0A%20%20%20%20nums%20%3D%20%5B0%5D%20*%20n%0A%20%20%20%20return%20quadratic_recur%28n%20-%201%29%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%20print%28%22%E8%BE%93%E5%85%A5%E6%95%B0%E6%8D%AE%E5%A4%A7%E5%B0%8F%20n%20%3D%22,%20n%29%0A%0A%20%20%20%20%23%20%E5%B9%B3%E6%96%B9%E9%98%B6%0A%20%20%20%20quadratic_recur%28n%29&codeDivHeight=472&codeDivWidth=350&cumulative=false&curInstr=28&heapPrimitives=nevernest&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false"> </iframe></div>
<div style="margin-top: 5px;"><a href="https://pythontutor.com/iframe-embed.html#code=def%20quadratic_recur%28n%3A%20int%29%20-%3E%20int%3A%0A%20%20%20%20%22%22%22%E5%B9%B3%E6%96%B9%E9%98%B6%EF%BC%88%E9%80%92%E5%BD%92%E5%AE%9E%E7%8E%B0%EF%BC%89%22%22%22%0A%20%20%20%20if%20n%20%3C%3D%200%3A%0A%20%20%20%20%20%20%20%20return%200%0A%20%20%20%20%23%20%E6%95%B0%E7%BB%84%20nums%20%E9%95%BF%E5%BA%A6%E4%B8%BA%20n,%20n-1,%20...,%202,%201%0A%20%20%20%20nums%20%3D%20%5B0%5D%20*%20n%0A%20%20%20%20return%20quadratic_recur%28n%20-%201%29%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%20print%28%22%E8%BE%93%E5%85%A5%E6%95%B0%E6%8D%AE%E5%A4%A7%E5%B0%8F%20n%20%3D%22,%20n%29%0A%0A%20%20%20%20%23%20%E5%B9%B3%E6%96%B9%E9%98%B6%0A%20%20%20%20quadratic_recur%28n%29&codeDivHeight=800&codeDivWidth=600&cumulative=false&curInstr=28&heapPrimitives=nevernest&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false" target="_blank" rel="noopener noreferrer">Full Screen &gt;</a></div></p>
</details>
<p><a class="glightbox" href="../space_complexity.assets/space_complexity_recursive_quadratic.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Recursive Function Generating Quadratic Order Space Complexity" class="animation-figure" src="../space_complexity.assets/space_complexity_recursive_quadratic.png" /></a></p>
<p align="center"> Figure 2-18 &nbsp; Recursive Function Generating Quadratic Order Space Complexity </p>
<p><a class="glightbox" href="../space_complexity.assets/space_complexity_recursive_quadratic.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Recursive function generating quadratic order space complexity" class="animation-figure" src="../space_complexity.assets/space_complexity_recursive_quadratic.png" /></a></p>
<p align="center"> Figure 2-18 &nbsp; Recursive function generating quadratic order space complexity </p>
<h3 id="4-exponential-order-o2n">4. &nbsp; Exponential Order <span class="arithmatex">\(O(2^n)\)</span><a class="headerlink" href="#4-exponential-order-o2n" title="Permanent link">&para;</a></h3>
<h3 id="4-exponential-order-o2n">4. &nbsp; Exponential order <span class="arithmatex">\(O(2^n)\)</span><a class="headerlink" href="#4-exponential-order-o2n" title="Permanent link">&para;</a></h3>
<p>Exponential order is common in binary trees. Observe the below image, a "full binary tree" with <span class="arithmatex">\(n\)</span> levels has <span class="arithmatex">\(2^n - 1\)</span> nodes, occupying <span class="arithmatex">\(O(2^n)\)</span> space:</p>
<div class="tabbed-set tabbed-alternate" data-tabs="9:14"><input checked="checked" id="__tabbed_9_1" name="__tabbed_9" type="radio" /><input id="__tabbed_9_2" name="__tabbed_9" type="radio" /><input id="__tabbed_9_3" name="__tabbed_9" type="radio" /><input id="__tabbed_9_4" name="__tabbed_9" type="radio" /><input id="__tabbed_9_5" name="__tabbed_9" type="radio" /><input id="__tabbed_9_6" name="__tabbed_9" type="radio" /><input id="__tabbed_9_7" name="__tabbed_9" type="radio" /><input id="__tabbed_9_8" name="__tabbed_9" type="radio" /><input id="__tabbed_9_9" name="__tabbed_9" type="radio" /><input id="__tabbed_9_10" name="__tabbed_9" type="radio" /><input id="__tabbed_9_11" name="__tabbed_9" type="radio" /><input id="__tabbed_9_12" name="__tabbed_9" type="radio" /><input id="__tabbed_9_13" name="__tabbed_9" type="radio" /><input id="__tabbed_9_14" name="__tabbed_9" type="radio" /><div class="tabbed-labels"><label for="__tabbed_9_1">Python</label><label for="__tabbed_9_2">C++</label><label for="__tabbed_9_3">Java</label><label for="__tabbed_9_4">C#</label><label for="__tabbed_9_5">Go</label><label for="__tabbed_9_6">Swift</label><label for="__tabbed_9_7">JS</label><label for="__tabbed_9_8">TS</label><label for="__tabbed_9_9">Dart</label><label for="__tabbed_9_10">Rust</label><label for="__tabbed_9_11">C</label><label for="__tabbed_9_12">Kotlin</label><label for="__tabbed_9_13">Ruby</label><label for="__tabbed_9_14">Zig</label></div>
<div class="tabbed-content">
@@ -4238,13 +4244,13 @@ O(1) &lt; O(\log n) &lt; O(n) &lt; O(n^2) &lt; O(2^n) \newline
<p><div style="height: 549px; width: 100%;"><iframe class="pythontutor-iframe" src="https://pythontutor.com/iframe-embed.html#code=class%20TreeNode%3A%0A%20%20%20%20%22%22%22%E4%BA%8C%E5%8F%89%E6%A0%91%E8%8A%82%E7%82%B9%E7%B1%BB%22%22%22%0A%20%20%20%20def%20__init__%28self,%20val%3A%20int%20%3D%200%29%3A%0A%20%20%20%20%20%20%20%20self.val%3A%20int%20%3D%20val%20%20%23%20%E8%8A%82%E7%82%B9%E5%80%BC%0A%20%20%20%20%20%20%20%20self.left%3A%20TreeNode%20%7C%20None%20%3D%20None%20%20%23%20%E5%B7%A6%E5%AD%90%E8%8A%82%E7%82%B9%E5%BC%95%E7%94%A8%0A%20%20%20%20%20%20%20%20self.right%3A%20TreeNode%20%7C%20None%20%3D%20None%20%20%23%20%E5%8F%B3%E5%AD%90%E8%8A%82%E7%82%B9%E5%BC%95%E7%94%A8%0A%0Adef%20build_tree%28n%3A%20int%29%20-%3E%20TreeNode%20%7C%20None%3A%0A%20%20%20%20%22%22%22%E6%8C%87%E6%95%B0%E9%98%B6%EF%BC%88%E5%BB%BA%E7%AB%8B%E6%BB%A1%E4%BA%8C%E5%8F%89%E6%A0%91%EF%BC%89%22%22%22%0A%20%20%20%20if%20n%20%3D%3D%200%3A%0A%20%20%20%20%20%20%20%20return%20None%0A%20%20%20%20root%20%3D%20TreeNode%280%29%0A%20%20%20%20root.left%20%3D%20build_tree%28n%20-%201%29%0A%20%20%20%20root.right%20%3D%20build_tree%28n%20-%201%29%0A%20%20%20%20return%20root%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%20print%28%22%E8%BE%93%E5%85%A5%E6%95%B0%E6%8D%AE%E5%A4%A7%E5%B0%8F%20n%20%3D%22,%20n%29%0A%0A%20%20%20%20%23%20%E6%8C%87%E6%95%B0%E9%98%B6%0A%20%20%20%20root%20%3D%20build_tree%28n%29&codeDivHeight=472&codeDivWidth=350&cumulative=false&curInstr=507&heapPrimitives=nevernest&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false"> </iframe></div>
<div style="margin-top: 5px;"><a href="https://pythontutor.com/iframe-embed.html#code=class%20TreeNode%3A%0A%20%20%20%20%22%22%22%E4%BA%8C%E5%8F%89%E6%A0%91%E8%8A%82%E7%82%B9%E7%B1%BB%22%22%22%0A%20%20%20%20def%20__init__%28self,%20val%3A%20int%20%3D%200%29%3A%0A%20%20%20%20%20%20%20%20self.val%3A%20int%20%3D%20val%20%20%23%20%E8%8A%82%E7%82%B9%E5%80%BC%0A%20%20%20%20%20%20%20%20self.left%3A%20TreeNode%20%7C%20None%20%3D%20None%20%20%23%20%E5%B7%A6%E5%AD%90%E8%8A%82%E7%82%B9%E5%BC%95%E7%94%A8%0A%20%20%20%20%20%20%20%20self.right%3A%20TreeNode%20%7C%20None%20%3D%20None%20%20%23%20%E5%8F%B3%E5%AD%90%E8%8A%82%E7%82%B9%E5%BC%95%E7%94%A8%0A%0Adef%20build_tree%28n%3A%20int%29%20-%3E%20TreeNode%20%7C%20None%3A%0A%20%20%20%20%22%22%22%E6%8C%87%E6%95%B0%E9%98%B6%EF%BC%88%E5%BB%BA%E7%AB%8B%E6%BB%A1%E4%BA%8C%E5%8F%89%E6%A0%91%EF%BC%89%22%22%22%0A%20%20%20%20if%20n%20%3D%3D%200%3A%0A%20%20%20%20%20%20%20%20return%20None%0A%20%20%20%20root%20%3D%20TreeNode%280%29%0A%20%20%20%20root.left%20%3D%20build_tree%28n%20-%201%29%0A%20%20%20%20root.right%20%3D%20build_tree%28n%20-%201%29%0A%20%20%20%20return%20root%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%20print%28%22%E8%BE%93%E5%85%A5%E6%95%B0%E6%8D%AE%E5%A4%A7%E5%B0%8F%20n%20%3D%22,%20n%29%0A%0A%20%20%20%20%23%20%E6%8C%87%E6%95%B0%E9%98%B6%0A%20%20%20%20root%20%3D%20build_tree%28n%29&codeDivHeight=800&codeDivWidth=600&cumulative=false&curInstr=507&heapPrimitives=nevernest&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false" target="_blank" rel="noopener noreferrer">Full Screen &gt;</a></div></p>
</details>
<p><a class="glightbox" href="../space_complexity.assets/space_complexity_exponential.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Full Binary Tree Generating Exponential Order Space Complexity" class="animation-figure" src="../space_complexity.assets/space_complexity_exponential.png" /></a></p>
<p align="center"> Figure 2-19 &nbsp; Full Binary Tree Generating Exponential Order Space Complexity </p>
<p><a class="glightbox" href="../space_complexity.assets/space_complexity_exponential.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Full binary tree generating exponential order space complexity" class="animation-figure" src="../space_complexity.assets/space_complexity_exponential.png" /></a></p>
<p align="center"> Figure 2-19 &nbsp; Full binary tree generating exponential order space complexity </p>
<h3 id="5-logarithmic-order-olog-n">5. &nbsp; Logarithmic Order <span class="arithmatex">\(O(\log n)\)</span><a class="headerlink" href="#5-logarithmic-order-olog-n" title="Permanent link">&para;</a></h3>
<h3 id="5-logarithmic-order-olog-n">5. &nbsp; Logarithmic order <span class="arithmatex">\(O(\log n)\)</span><a class="headerlink" href="#5-logarithmic-order-olog-n" title="Permanent link">&para;</a></h3>
<p>Logarithmic order is common in divide-and-conquer algorithms. For example, in merge sort, an array of length <span class="arithmatex">\(n\)</span> is recursively divided in half each round, forming a recursion tree of height <span class="arithmatex">\(\log n\)</span>, using <span class="arithmatex">\(O(\log n)\)</span> stack frame space.</p>
<p>Another example is converting a number to a string. Given a positive integer <span class="arithmatex">\(n\)</span>, its number of digits is <span class="arithmatex">\(\log_{10} n + 1\)</span>, corresponding to the length of the string, thus the space complexity is <span class="arithmatex">\(O(\log_{10} n + 1) = O(\log n)\)</span>.</p>
<h2 id="244-balancing-time-and-space">2.4.4 &nbsp; Balancing Time and Space<a class="headerlink" href="#244-balancing-time-and-space" title="Permanent link">&para;</a></h2>
<h2 id="244-balancing-time-and-space">2.4.4 &nbsp; Balancing time and space<a class="headerlink" href="#244-balancing-time-and-space" title="Permanent link">&para;</a></h2>
<p>Ideally, we aim for both time complexity and space complexity to be optimal. However, in practice, optimizing both simultaneously is often difficult.</p>
<p><strong>Lowering time complexity usually comes at the cost of increased space complexity, and vice versa</strong>. The approach of sacrificing memory space to improve algorithm speed is known as "space-time tradeoff"; the reverse is known as "time-space tradeoff".</p>
<p>The choice depends on which aspect we value more. In most cases, time is more precious than space, so "space-time tradeoff" is often the more common strategy. Of course, controlling space complexity is also very important when dealing with large volumes of data.</p>
@@ -4270,7 +4276,7 @@ aria-label="Footer"
<a
href="../time_complexity/"
class="md-footer__link md-footer__link--prev"
aria-label="Previous: 2.3 Time Complexity"
aria-label="Previous: 2.3 Time complexity"
rel="prev"
>
<div class="md-footer__button md-icon">
@@ -4282,7 +4288,7 @@ aria-label="Footer"
Previous
</span>
<div class="md-ellipsis">
2.3 Time Complexity
2.3 Time complexity
</div>
</div>
</a>
@@ -4395,7 +4401,7 @@ aria-label="Footer"
<nav class="md-footer__inner md-grid" aria-label="Footer" >
<a href="../time_complexity/" class="md-footer__link md-footer__link--prev" aria-label="Previous: 2.3 Time Complexity">
<a href="../time_complexity/" class="md-footer__link md-footer__link--prev" aria-label="Previous: 2.3 Time complexity">
<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>
@@ -4405,7 +4411,7 @@ aria-label="Footer"
Previous
</span>
<div class="md-ellipsis">
2.3 Time Complexity
2.3 Time complexity
</div>
</div>
</a>
@@ -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>
@@ -682,7 +688,7 @@
<span class="md-ellipsis">
2.2 Iteration and Recursion
2.2 Iteration and recursion
</span>
@@ -703,7 +709,7 @@
<span class="md-ellipsis">
2.3 Time Complexity
2.3 Time complexity
</span>
@@ -724,7 +730,7 @@
<span class="md-ellipsis">
2.4 Space Complexity
2.4 Space complexity
</span>
@@ -789,7 +795,7 @@
<li class="md-nav__item">
<a href="#1-key-review" class="md-nav__link">
<span class="md-ellipsis">
1. &nbsp; Key Review
1. &nbsp; Key review
</span>
</a>
@@ -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="#1-key-review" class="md-nav__link">
<span class="md-ellipsis">
1. &nbsp; Key Review
1. &nbsp; Key review
</span>
</a>
@@ -2071,7 +2077,7 @@
<!-- Page content -->
<h1 id="25-summary">2.5 &nbsp; Summary<a class="headerlink" href="#25-summary" title="Permanent link">&para;</a></h1>
<h3 id="1-key-review">1. &nbsp; Key Review<a class="headerlink" href="#1-key-review" title="Permanent link">&para;</a></h3>
<h3 id="1-key-review">1. &nbsp; Key review<a class="headerlink" href="#1-key-review" title="Permanent link">&para;</a></h3>
<p><strong>Algorithm Efficiency Assessment</strong></p>
<ul>
<li>Time efficiency and space efficiency are the two main criteria for assessing the merits of an algorithm.</li>
@@ -2131,7 +2137,7 @@ aria-label="Footer"
<a
href="../space_complexity/"
class="md-footer__link md-footer__link--prev"
aria-label="Previous: 2.4 Space Complexity"
aria-label="Previous: 2.4 Space complexity"
rel="prev"
>
<div class="md-footer__button md-icon">
@@ -2143,7 +2149,7 @@ aria-label="Footer"
Previous
</span>
<div class="md-ellipsis">
2.4 Space Complexity
2.4 Space complexity
</div>
</div>
</a>
@@ -2155,7 +2161,7 @@ aria-label="Footer"
<a
href="../../chapter_data_structure/"
class="md-footer__link md-footer__link--next"
aria-label="Next: Chapter 3. &amp;nbsp; Data Structures"
aria-label="Next: Chapter 3. &amp;nbsp; Data structures"
rel="next"
>
<div class="md-footer__title">
@@ -2163,7 +2169,7 @@ aria-label="Footer"
Next
</span>
<div class="md-ellipsis">
Chapter 3. &nbsp; Data Structures
Chapter 3. &nbsp; Data structures
</div>
</div>
<div class="md-footer__button md-icon">
@@ -2256,7 +2262,7 @@ aria-label="Footer"
<nav class="md-footer__inner md-grid" aria-label="Footer" >
<a href="../space_complexity/" class="md-footer__link md-footer__link--prev" aria-label="Previous: 2.4 Space Complexity">
<a href="../space_complexity/" class="md-footer__link md-footer__link--prev" aria-label="Previous: 2.4 Space complexity">
<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>
@@ -2266,20 +2272,20 @@ aria-label="Footer"
Previous
</span>
<div class="md-ellipsis">
2.4 Space Complexity
2.4 Space complexity
</div>
</div>
</a>
<a href="../../chapter_data_structure/" class="md-footer__link md-footer__link--next" aria-label="Next: Chapter 3. &amp;nbsp; Data Structures">
<a href="../../chapter_data_structure/" class="md-footer__link md-footer__link--next" aria-label="Next: Chapter 3. &amp;nbsp; Data structures">
<div class="md-footer__title">
<span class="md-footer__direction">
Next
</span>
<div class="md-ellipsis">
Chapter 3. &nbsp; Data Structures
Chapter 3. &nbsp; Data structures
</div>
</div>
<div class="md-footer__button md-icon">
@@ -26,7 +26,7 @@
<title>2.3 Time Complexity - Hello Algo</title>
<title>2.3 Time complexity - Hello Algo</title>
@@ -153,7 +153,7 @@
<div class="md-header__topic" data-md-component="header-topic">
<span class="md-ellipsis">
2.3 Time Complexity
2.3 Time complexity
</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>
@@ -682,7 +688,7 @@
<span class="md-ellipsis">
2.2 Iteration and Recursion
2.2 Iteration and recursion
</span>
@@ -712,7 +718,7 @@
<span class="md-ellipsis">
2.3 Time Complexity
2.3 Time complexity
</span>
@@ -723,7 +729,7 @@
<span class="md-ellipsis">
2.3 Time Complexity
2.3 Time complexity
</span>
@@ -747,7 +753,7 @@
<li class="md-nav__item">
<a href="#231-assessing-time-growth-trend" class="md-nav__link">
<span class="md-ellipsis">
2.3.1 &nbsp; Assessing Time Growth Trend
2.3.1 &nbsp; Assessing time growth trend
</span>
</a>
@@ -756,7 +762,7 @@
<li class="md-nav__item">
<a href="#232-asymptotic-upper-bound" class="md-nav__link">
<span class="md-ellipsis">
2.3.2 &nbsp; Asymptotic Upper Bound
2.3.2 &nbsp; Asymptotic upper bound
</span>
</a>
@@ -765,17 +771,17 @@
<li class="md-nav__item">
<a href="#233-calculation-method" class="md-nav__link">
<span class="md-ellipsis">
2.3.3 &nbsp; Calculation Method
2.3.3 &nbsp; Calculation method
</span>
</a>
<nav class="md-nav" aria-label="2.3.3   Calculation Method">
<nav class="md-nav" aria-label="2.3.3   Calculation method">
<ul class="md-nav__list">
<li class="md-nav__item">
<a href="#1-step-1-counting-the-number-of-operations" class="md-nav__link">
<span class="md-ellipsis">
1. &nbsp; Step 1: Counting the Number of Operations
1. &nbsp; Step 1: counting the number of operations
</span>
</a>
@@ -784,7 +790,7 @@
<li class="md-nav__item">
<a href="#2-step-2-determining-the-asymptotic-upper-bound" class="md-nav__link">
<span class="md-ellipsis">
2. &nbsp; Step 2: Determining the Asymptotic Upper Bound
2. &nbsp; Step 2: determining the asymptotic upper bound
</span>
</a>
@@ -798,17 +804,17 @@
<li class="md-nav__item">
<a href="#234-common-types-of-time-complexity" class="md-nav__link">
<span class="md-ellipsis">
2.3.4 &nbsp; Common Types of Time Complexity
2.3.4 &nbsp; Common types of time complexity
</span>
</a>
<nav class="md-nav" aria-label="2.3.4   Common Types of Time Complexity">
<nav class="md-nav" aria-label="2.3.4   Common types of time complexity">
<ul class="md-nav__list">
<li class="md-nav__item">
<a href="#1-constant-order-o1" class="md-nav__link">
<span class="md-ellipsis">
1. &nbsp; Constant Order
1. &nbsp; Constant order
</span>
</a>
@@ -817,7 +823,7 @@
<li class="md-nav__item">
<a href="#2-linear-order-on" class="md-nav__link">
<span class="md-ellipsis">
2. &nbsp; Linear Order
2. &nbsp; Linear order
</span>
</a>
@@ -826,7 +832,7 @@
<li class="md-nav__item">
<a href="#3-quadratic-order-on2" class="md-nav__link">
<span class="md-ellipsis">
3. &nbsp; Quadratic Order
3. &nbsp; Quadratic order
</span>
</a>
@@ -835,7 +841,7 @@
<li class="md-nav__item">
<a href="#4-exponential-order-o2n" class="md-nav__link">
<span class="md-ellipsis">
4. &nbsp; Exponential Order
4. &nbsp; Exponential order
</span>
</a>
@@ -844,7 +850,7 @@
<li class="md-nav__item">
<a href="#5-logarithmic-order-olog-n" class="md-nav__link">
<span class="md-ellipsis">
5. &nbsp; Logarithmic Order
5. &nbsp; Logarithmic order
</span>
</a>
@@ -853,7 +859,7 @@
<li class="md-nav__item">
<a href="#6-linear-logarithmic-order-on-log-n" class="md-nav__link">
<span class="md-ellipsis">
6. &nbsp; Linear-Logarithmic Order
6. &nbsp; Linear-logarithmic order
</span>
</a>
@@ -862,7 +868,7 @@
<li class="md-nav__item">
<a href="#7-factorial-order-on" class="md-nav__link">
<span class="md-ellipsis">
7. &nbsp; Factorial Order
7. &nbsp; Factorial order
</span>
</a>
@@ -876,7 +882,7 @@
<li class="md-nav__item">
<a href="#235-worst-best-and-average-time-complexities" class="md-nav__link">
<span class="md-ellipsis">
2.3.5 &nbsp; Worst, Best, and Average Time Complexities
2.3.5 &nbsp; Worst, best, and average time complexities
</span>
</a>
@@ -902,7 +908,7 @@
<span class="md-ellipsis">
2.4 Space Complexity
2.4 Space complexity
</span>
@@ -983,7 +989,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>
@@ -999,7 +1005,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>
@@ -1016,7 +1022,7 @@
<span class="md-ellipsis">
3.1 Classification of Data Structures
3.1 Classification of data structures
</span>
@@ -1037,7 +1043,7 @@
<span class="md-ellipsis">
3.2 Fundamental Data Types
3.2 Fundamental data types
</span>
@@ -1058,7 +1064,7 @@
<span class="md-ellipsis">
3.3 Number Encoding *
3.3 Number encoding *
</span>
@@ -1079,7 +1085,7 @@
<span class="md-ellipsis">
3.4 Character Encoding *
3.4 Character encoding *
</span>
@@ -1160,7 +1166,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>
@@ -1176,7 +1182,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>
@@ -1214,7 +1220,7 @@
<span class="md-ellipsis">
4.2 Linked List
4.2 Linked list
</span>
@@ -1256,7 +1262,7 @@
<span class="md-ellipsis">
4.4 Memory and Cache
4.4 Memory and cache
</span>
@@ -1335,7 +1341,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>
@@ -1351,7 +1357,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>
@@ -1410,7 +1416,7 @@
<span class="md-ellipsis">
5.3 Double-ended Queue
5.3 Double-ended queue
</span>
@@ -1489,7 +1495,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>
@@ -1505,7 +1511,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>
@@ -1522,7 +1528,7 @@
<span class="md-ellipsis">
6.1 Hash Table
6.1 Hash table
</span>
@@ -1543,7 +1549,7 @@
<span class="md-ellipsis">
6.2 Hash Collision
6.2 Hash collision
</span>
@@ -1564,7 +1570,7 @@
<span class="md-ellipsis">
6.3 Hash Algorithm
6.3 Hash algorithm
</span>
@@ -2139,7 +2145,7 @@
<li class="md-nav__item">
<a href="#231-assessing-time-growth-trend" class="md-nav__link">
<span class="md-ellipsis">
2.3.1 &nbsp; Assessing Time Growth Trend
2.3.1 &nbsp; Assessing time growth trend
</span>
</a>
@@ -2148,7 +2154,7 @@
<li class="md-nav__item">
<a href="#232-asymptotic-upper-bound" class="md-nav__link">
<span class="md-ellipsis">
2.3.2 &nbsp; Asymptotic Upper Bound
2.3.2 &nbsp; Asymptotic upper bound
</span>
</a>
@@ -2157,17 +2163,17 @@
<li class="md-nav__item">
<a href="#233-calculation-method" class="md-nav__link">
<span class="md-ellipsis">
2.3.3 &nbsp; Calculation Method
2.3.3 &nbsp; Calculation method
</span>
</a>
<nav class="md-nav" aria-label="2.3.3   Calculation Method">
<nav class="md-nav" aria-label="2.3.3   Calculation method">
<ul class="md-nav__list">
<li class="md-nav__item">
<a href="#1-step-1-counting-the-number-of-operations" class="md-nav__link">
<span class="md-ellipsis">
1. &nbsp; Step 1: Counting the Number of Operations
1. &nbsp; Step 1: counting the number of operations
</span>
</a>
@@ -2176,7 +2182,7 @@
<li class="md-nav__item">
<a href="#2-step-2-determining-the-asymptotic-upper-bound" class="md-nav__link">
<span class="md-ellipsis">
2. &nbsp; Step 2: Determining the Asymptotic Upper Bound
2. &nbsp; Step 2: determining the asymptotic upper bound
</span>
</a>
@@ -2190,17 +2196,17 @@
<li class="md-nav__item">
<a href="#234-common-types-of-time-complexity" class="md-nav__link">
<span class="md-ellipsis">
2.3.4 &nbsp; Common Types of Time Complexity
2.3.4 &nbsp; Common types of time complexity
</span>
</a>
<nav class="md-nav" aria-label="2.3.4   Common Types of Time Complexity">
<nav class="md-nav" aria-label="2.3.4   Common types of time complexity">
<ul class="md-nav__list">
<li class="md-nav__item">
<a href="#1-constant-order-o1" class="md-nav__link">
<span class="md-ellipsis">
1. &nbsp; Constant Order
1. &nbsp; Constant order
</span>
</a>
@@ -2209,7 +2215,7 @@
<li class="md-nav__item">
<a href="#2-linear-order-on" class="md-nav__link">
<span class="md-ellipsis">
2. &nbsp; Linear Order
2. &nbsp; Linear order
</span>
</a>
@@ -2218,7 +2224,7 @@
<li class="md-nav__item">
<a href="#3-quadratic-order-on2" class="md-nav__link">
<span class="md-ellipsis">
3. &nbsp; Quadratic Order
3. &nbsp; Quadratic order
</span>
</a>
@@ -2227,7 +2233,7 @@
<li class="md-nav__item">
<a href="#4-exponential-order-o2n" class="md-nav__link">
<span class="md-ellipsis">
4. &nbsp; Exponential Order
4. &nbsp; Exponential order
</span>
</a>
@@ -2236,7 +2242,7 @@
<li class="md-nav__item">
<a href="#5-logarithmic-order-olog-n" class="md-nav__link">
<span class="md-ellipsis">
5. &nbsp; Logarithmic Order
5. &nbsp; Logarithmic order
</span>
</a>
@@ -2245,7 +2251,7 @@
<li class="md-nav__item">
<a href="#6-linear-logarithmic-order-on-log-n" class="md-nav__link">
<span class="md-ellipsis">
6. &nbsp; Linear-Logarithmic Order
6. &nbsp; Linear-logarithmic order
</span>
</a>
@@ -2254,7 +2260,7 @@
<li class="md-nav__item">
<a href="#7-factorial-order-on" class="md-nav__link">
<span class="md-ellipsis">
7. &nbsp; Factorial Order
7. &nbsp; Factorial order
</span>
</a>
@@ -2268,7 +2274,7 @@
<li class="md-nav__item">
<a href="#235-worst-best-and-average-time-complexities" class="md-nav__link">
<span class="md-ellipsis">
2.3.5 &nbsp; Worst, Best, and Average Time Complexities
2.3.5 &nbsp; Worst, best, and average time complexities
</span>
</a>
@@ -2310,7 +2316,7 @@
<!-- Page content -->
<h1 id="23-time-complexity">2.3 &nbsp; Time Complexity<a class="headerlink" href="#23-time-complexity" title="Permanent link">&para;</a></h1>
<h1 id="23-time-complexity">2.3 &nbsp; Time complexity<a class="headerlink" href="#23-time-complexity" title="Permanent link">&para;</a></h1>
<p>Time complexity is a concept used to measure how the run time of an algorithm increases with the size of the input data. Understanding time complexity is crucial for accurately assessing the efficiency of an algorithm.</p>
<ol>
<li><strong>Determining the Running Platform</strong>: This includes hardware configuration, programming language, system environment, etc., all of which can affect the efficiency of code execution.</li>
@@ -2485,7 +2491,7 @@
1 + 1 + 10 + (1 + 5) \times n = 6n + 12
\]</div>
<p>However, in practice, <strong>counting the run time of an algorithm is neither practical nor reasonable</strong>. First, we don't want to tie the estimated time to the running platform, as algorithms need to run on various platforms. Second, it's challenging to know the run time for each type of operation, making the estimation process difficult.</p>
<h2 id="231-assessing-time-growth-trend">2.3.1 &nbsp; Assessing Time Growth Trend<a class="headerlink" href="#231-assessing-time-growth-trend" title="Permanent link">&para;</a></h2>
<h2 id="231-assessing-time-growth-trend">2.3.1 &nbsp; Assessing time growth trend<a class="headerlink" href="#231-assessing-time-growth-trend" title="Permanent link">&para;</a></h2>
<p>Time complexity analysis does not count the algorithm's run time, <strong>but rather the growth trend of the run time as the data volume increases</strong>.</p>
<p>Let's understand this concept of "time growth trend" with an example. Assume the input data size is <span class="arithmatex">\(n\)</span>, and consider three algorithms <code>A</code>, <code>B</code>, and <code>C</code>:</p>
<div class="tabbed-set tabbed-alternate" data-tabs="2:13"><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" /><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">Zig</label></div>
@@ -2729,8 +2735,8 @@
<li>Algorithm <code>B</code> involves a print operation looping <span class="arithmatex">\(n\)</span> times, and its run time grows linearly with <span class="arithmatex">\(n\)</span>. Its time complexity is "linear order."</li>
<li>Algorithm <code>C</code> has a print operation looping 1,000,000 times. Although it takes a long time, it is independent of the input data size <span class="arithmatex">\(n\)</span>. Therefore, the time complexity of <code>C</code> is the same as <code>A</code>, which is "constant order."</li>
</ul>
<p><a class="glightbox" href="../time_complexity.assets/time_complexity_simple_example.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Time Growth Trend of Algorithms A, B, and C" class="animation-figure" src="../time_complexity.assets/time_complexity_simple_example.png" /></a></p>
<p align="center"> Figure 2-7 &nbsp; Time Growth Trend of Algorithms A, B, and C </p>
<p><a class="glightbox" href="../time_complexity.assets/time_complexity_simple_example.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Time growth trend of algorithms a, b, and c" class="animation-figure" src="../time_complexity.assets/time_complexity_simple_example.png" /></a></p>
<p align="center"> Figure 2-7 &nbsp; Time growth trend of algorithms a, b, and c </p>
<p>Compared to directly counting the run time of an algorithm, what are the characteristics of time complexity analysis?</p>
<ul>
@@ -2738,7 +2744,7 @@
<li><strong>Time complexity analysis is more straightforward</strong>. Obviously, the running platform and the types of computational operations are irrelevant to the trend of run time growth. Therefore, in time complexity analysis, we can simply treat the execution time of all computational operations as the same "unit time," simplifying the "computational operation run time count" to a "computational operation count." This significantly reduces the complexity of estimation.</li>
<li><strong>Time complexity has its limitations</strong>. For example, although algorithms <code>A</code> and <code>C</code> have the same time complexity, their actual run times can be quite different. Similarly, even though algorithm <code>B</code> has a higher time complexity than <code>C</code>, it is clearly superior when the input data size <span class="arithmatex">\(n\)</span> is small. In these cases, it's difficult to judge the efficiency of algorithms based solely on time complexity. Nonetheless, despite these issues, complexity analysis remains the most effective and commonly used method for evaluating algorithm efficiency.</li>
</ul>
<h2 id="232-asymptotic-upper-bound">2.3.2 &nbsp; Asymptotic Upper Bound<a class="headerlink" href="#232-asymptotic-upper-bound" title="Permanent link">&para;</a></h2>
<h2 id="232-asymptotic-upper-bound">2.3.2 &nbsp; Asymptotic upper bound<a class="headerlink" href="#232-asymptotic-upper-bound" title="Permanent link">&para;</a></h2>
<p>Consider a function with an input size of <span class="arithmatex">\(n\)</span>:</p>
<div class="tabbed-set tabbed-alternate" data-tabs="3:13"><input checked="checked" id="__tabbed_3_1" name="__tabbed_3" type="radio" /><input id="__tabbed_3_2" name="__tabbed_3" type="radio" /><input id="__tabbed_3_3" name="__tabbed_3" type="radio" /><input id="__tabbed_3_4" name="__tabbed_3" type="radio" /><input id="__tabbed_3_5" name="__tabbed_3" type="radio" /><input id="__tabbed_3_6" name="__tabbed_3" type="radio" /><input id="__tabbed_3_7" name="__tabbed_3" type="radio" /><input id="__tabbed_3_8" name="__tabbed_3" type="radio" /><input id="__tabbed_3_9" name="__tabbed_3" type="radio" /><input id="__tabbed_3_10" name="__tabbed_3" type="radio" /><input id="__tabbed_3_11" name="__tabbed_3" type="radio" /><input id="__tabbed_3_12" name="__tabbed_3" type="radio" /><input id="__tabbed_3_13" name="__tabbed_3" type="radio" /><div class="tabbed-labels"><label for="__tabbed_3_1">Python</label><label for="__tabbed_3_2">C++</label><label for="__tabbed_3_3">Java</label><label for="__tabbed_3_4">C#</label><label for="__tabbed_3_5">Go</label><label for="__tabbed_3_6">Swift</label><label for="__tabbed_3_7">JS</label><label for="__tabbed_3_8">TS</label><label for="__tabbed_3_9">Dart</label><label for="__tabbed_3_10">Rust</label><label for="__tabbed_3_11">C</label><label for="__tabbed_3_12">Kotlin</label><label for="__tabbed_3_13">Zig</label></div>
<div class="tabbed-content">
@@ -2902,13 +2908,13 @@ T(n) = 3 + 2n
<p>If there exist positive real numbers <span class="arithmatex">\(c\)</span> and <span class="arithmatex">\(n_0\)</span> such that for all <span class="arithmatex">\(n &gt; n_0\)</span>, <span class="arithmatex">\(T(n) \leq c \cdot f(n)\)</span>, then <span class="arithmatex">\(f(n)\)</span> is considered an asymptotic upper bound of <span class="arithmatex">\(T(n)\)</span>, denoted as <span class="arithmatex">\(T(n) = O(f(n))\)</span>.</p>
</div>
<p>As illustrated below, calculating the asymptotic upper bound involves finding a function <span class="arithmatex">\(f(n)\)</span> such that, as <span class="arithmatex">\(n\)</span> approaches infinity, <span class="arithmatex">\(T(n)\)</span> and <span class="arithmatex">\(f(n)\)</span> have the same growth order, differing only by a constant factor <span class="arithmatex">\(c\)</span>.</p>
<p><a class="glightbox" href="../time_complexity.assets/asymptotic_upper_bound.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Asymptotic Upper Bound of a Function" class="animation-figure" src="../time_complexity.assets/asymptotic_upper_bound.png" /></a></p>
<p align="center"> Figure 2-8 &nbsp; Asymptotic Upper Bound of a Function </p>
<p><a class="glightbox" href="../time_complexity.assets/asymptotic_upper_bound.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Asymptotic upper bound of a function" class="animation-figure" src="../time_complexity.assets/asymptotic_upper_bound.png" /></a></p>
<p align="center"> Figure 2-8 &nbsp; Asymptotic upper bound of a function </p>
<h2 id="233-calculation-method">2.3.3 &nbsp; Calculation Method<a class="headerlink" href="#233-calculation-method" title="Permanent link">&para;</a></h2>
<h2 id="233-calculation-method">2.3.3 &nbsp; Calculation method<a class="headerlink" href="#233-calculation-method" title="Permanent link">&para;</a></h2>
<p>While the concept of asymptotic upper bound might seem mathematically dense, you don't need to fully grasp it right away. Let's first understand the method of calculation, which can be practiced and comprehended over time.</p>
<p>Once <span class="arithmatex">\(f(n)\)</span> is determined, we obtain the time complexity <span class="arithmatex">\(O(f(n))\)</span>. But how do we determine the asymptotic upper bound <span class="arithmatex">\(f(n)\)</span>? This process generally involves two steps: counting the number of operations and determining the asymptotic upper bound.</p>
<h3 id="1-step-1-counting-the-number-of-operations">1. &nbsp; Step 1: Counting the Number of Operations<a class="headerlink" href="#1-step-1-counting-the-number-of-operations" title="Permanent link">&para;</a></h3>
<h3 id="1-step-1-counting-the-number-of-operations">1. &nbsp; Step 1: counting the number of operations<a class="headerlink" href="#1-step-1-counting-the-number-of-operations" title="Permanent link">&para;</a></h3>
<p>This step involves going through the code line by line. However, due to the presence of the constant <span class="arithmatex">\(c\)</span> in <span class="arithmatex">\(c \cdot f(n)\)</span>, <strong>all coefficients and constant terms in <span class="arithmatex">\(T(n)\)</span> can be ignored</strong>. This principle allows for simplification techniques in counting operations.</p>
<ol>
<li><strong>Ignore constant terms in <span class="arithmatex">\(T(n)\)</span></strong>, as they do not affect the time complexity being independent of <span class="arithmatex">\(n\)</span>.</li>
@@ -3136,10 +3142,10 @@ T(n) &amp; = 2n(n + 1) + (5n + 1) + 2 &amp; \text{Complete Count (-.-|||)} \newl
T(n) &amp; = n^2 + n &amp; \text{Simplified Count (o.O)}
\end{aligned}
\]</div>
<h3 id="2-step-2-determining-the-asymptotic-upper-bound">2. &nbsp; Step 2: Determining the Asymptotic Upper Bound<a class="headerlink" href="#2-step-2-determining-the-asymptotic-upper-bound" title="Permanent link">&para;</a></h3>
<h3 id="2-step-2-determining-the-asymptotic-upper-bound">2. &nbsp; Step 2: determining the asymptotic upper bound<a class="headerlink" href="#2-step-2-determining-the-asymptotic-upper-bound" title="Permanent link">&para;</a></h3>
<p><strong>The time complexity is determined by the highest order term in <span class="arithmatex">\(T(n)\)</span></strong>. This is because, as <span class="arithmatex">\(n\)</span> approaches infinity, the highest order term dominates, rendering the influence of other terms negligible.</p>
<p>The following table illustrates examples of different operation counts and their corresponding time complexities. Some exaggerated values are used to emphasize that coefficients cannot alter the order of growth. When <span class="arithmatex">\(n\)</span> becomes very large, these constants become insignificant.</p>
<p align="center"> Table: Time Complexity for Different Operation Counts </p>
<p align="center"> Table: Time complexity for different operation counts </p>
<div class="center-table">
<table>
@@ -3173,7 +3179,7 @@ T(n) &amp; = n^2 + n &amp; \text{Simplified Count (o.O)}
</tbody>
</table>
</div>
<h2 id="234-common-types-of-time-complexity">2.3.4 &nbsp; Common Types of Time Complexity<a class="headerlink" href="#234-common-types-of-time-complexity" title="Permanent link">&para;</a></h2>
<h2 id="234-common-types-of-time-complexity">2.3.4 &nbsp; Common types of time complexity<a class="headerlink" href="#234-common-types-of-time-complexity" title="Permanent link">&para;</a></h2>
<p>Let's consider the input data size as <span class="arithmatex">\(n\)</span>. The common types of time complexities are illustrated below, arranged from lowest to highest:</p>
<div class="arithmatex">\[
\begin{aligned}
@@ -3181,10 +3187,10 @@ O(1) &lt; O(\log n) &lt; O(n) &lt; O(n \log n) &lt; O(n^2) &lt; O(2^n) &lt; O(n!
\text{Constant Order} &lt; \text{Logarithmic Order} &lt; \text{Linear Order} &lt; \text{Linear-Logarithmic Order} &lt; \text{Quadratic Order} &lt; \text{Exponential Order} &lt; \text{Factorial Order}
\end{aligned}
\]</div>
<p><a class="glightbox" href="../time_complexity.assets/time_complexity_common_types.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Common Types of Time Complexity" class="animation-figure" src="../time_complexity.assets/time_complexity_common_types.png" /></a></p>
<p align="center"> Figure 2-9 &nbsp; Common Types of Time Complexity </p>
<p><a class="glightbox" href="../time_complexity.assets/time_complexity_common_types.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Common types of time complexity" class="animation-figure" src="../time_complexity.assets/time_complexity_common_types.png" /></a></p>
<p align="center"> Figure 2-9 &nbsp; Common types of time complexity </p>
<h3 id="1-constant-order-o1">1. &nbsp; Constant Order <span class="arithmatex">\(O(1)\)</span><a class="headerlink" href="#1-constant-order-o1" title="Permanent link">&para;</a></h3>
<h3 id="1-constant-order-o1">1. &nbsp; Constant order <span class="arithmatex">\(O(1)\)</span><a class="headerlink" href="#1-constant-order-o1" title="Permanent link">&para;</a></h3>
<p>Constant order means the number of operations is independent of the input data size <span class="arithmatex">\(n\)</span>. In the following function, although the number of operations <code>size</code> might be large, the time complexity remains <span class="arithmatex">\(O(1)\)</span> as it's unrelated to <span class="arithmatex">\(n\)</span>:</p>
<div class="tabbed-set tabbed-alternate" data-tabs="5:14"><input checked="checked" id="__tabbed_5_1" name="__tabbed_5" type="radio" /><input id="__tabbed_5_2" name="__tabbed_5" type="radio" /><input id="__tabbed_5_3" name="__tabbed_5" type="radio" /><input id="__tabbed_5_4" name="__tabbed_5" type="radio" /><input id="__tabbed_5_5" name="__tabbed_5" type="radio" /><input id="__tabbed_5_6" name="__tabbed_5" type="radio" /><input id="__tabbed_5_7" name="__tabbed_5" type="radio" /><input id="__tabbed_5_8" name="__tabbed_5" type="radio" /><input id="__tabbed_5_9" name="__tabbed_5" type="radio" /><input id="__tabbed_5_10" name="__tabbed_5" type="radio" /><input id="__tabbed_5_11" name="__tabbed_5" type="radio" /><input id="__tabbed_5_12" name="__tabbed_5" type="radio" /><input id="__tabbed_5_13" name="__tabbed_5" type="radio" /><input id="__tabbed_5_14" name="__tabbed_5" type="radio" /><div class="tabbed-labels"><label for="__tabbed_5_1">Python</label><label for="__tabbed_5_2">C++</label><label for="__tabbed_5_3">Java</label><label for="__tabbed_5_4">C#</label><label for="__tabbed_5_5">Go</label><label for="__tabbed_5_6">Swift</label><label for="__tabbed_5_7">JS</label><label for="__tabbed_5_8">TS</label><label for="__tabbed_5_9">Dart</label><label for="__tabbed_5_10">Rust</label><label for="__tabbed_5_11">C</label><label for="__tabbed_5_12">Kotlin</label><label for="__tabbed_5_13">Ruby</label><label for="__tabbed_5_14">Zig</label></div>
<div class="tabbed-content">
@@ -3357,7 +3363,7 @@ O(1) &lt; O(\log n) &lt; O(n) &lt; O(n \log n) &lt; O(n^2) &lt; O(2^n) &lt; O(n!
<p><div style="height: 459px; width: 100%;"><iframe class="pythontutor-iframe" src="https://pythontutor.com/iframe-embed.html#code=def%20constant%28n%3A%20int%29%20-%3E%20int%3A%0A%20%20%20%20%22%22%22%E5%B8%B8%E6%95%B0%E9%98%B6%22%22%22%0A%20%20%20%20count%20%3D%200%0A%20%20%20%20size%20%3D%2010%0A%20%20%20%20for%20_%20in%20range%28size%29%3A%0A%20%20%20%20%20%20%20%20count%20%2B%3D%201%0A%20%20%20%20return%20count%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%208%0A%20%20%20%20print%28%22%E8%BE%93%E5%85%A5%E6%95%B0%E6%8D%AE%E5%A4%A7%E5%B0%8F%20n%20%3D%22,%20n%29%0A%0A%20%20%20%20count%20%3D%20constant%28n%29%0A%20%20%20%20print%28%22%E5%B8%B8%E6%95%B0%E9%98%B6%E7%9A%84%E6%93%8D%E4%BD%9C%E6%95%B0%E9%87%8F%20%3D%22,%20count%29&codeDivHeight=472&codeDivWidth=350&cumulative=false&curInstr=3&heapPrimitives=nevernest&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false"> </iframe></div>
<div style="margin-top: 5px;"><a href="https://pythontutor.com/iframe-embed.html#code=def%20constant%28n%3A%20int%29%20-%3E%20int%3A%0A%20%20%20%20%22%22%22%E5%B8%B8%E6%95%B0%E9%98%B6%22%22%22%0A%20%20%20%20count%20%3D%200%0A%20%20%20%20size%20%3D%2010%0A%20%20%20%20for%20_%20in%20range%28size%29%3A%0A%20%20%20%20%20%20%20%20count%20%2B%3D%201%0A%20%20%20%20return%20count%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%208%0A%20%20%20%20print%28%22%E8%BE%93%E5%85%A5%E6%95%B0%E6%8D%AE%E5%A4%A7%E5%B0%8F%20n%20%3D%22,%20n%29%0A%0A%20%20%20%20count%20%3D%20constant%28n%29%0A%20%20%20%20print%28%22%E5%B8%B8%E6%95%B0%E9%98%B6%E7%9A%84%E6%93%8D%E4%BD%9C%E6%95%B0%E9%87%8F%20%3D%22,%20count%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 &gt;</a></div></p>
</details>
<h3 id="2-linear-order-on">2. &nbsp; Linear Order <span class="arithmatex">\(O(n)\)</span><a class="headerlink" href="#2-linear-order-on" title="Permanent link">&para;</a></h3>
<h3 id="2-linear-order-on">2. &nbsp; Linear order <span class="arithmatex">\(O(n)\)</span><a class="headerlink" href="#2-linear-order-on" title="Permanent link">&para;</a></h3>
<p>Linear order indicates the number of operations grows linearly with the input data size <span class="arithmatex">\(n\)</span>. Linear order commonly appears in single-loop structures:</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>
<div class="tabbed-content">
@@ -3691,7 +3697,7 @@ O(1) &lt; O(\log n) &lt; O(n) &lt; O(n \log n) &lt; O(n^2) &lt; O(2^n) &lt; O(n!
<div style="margin-top: 5px;"><a href="https://pythontutor.com/iframe-embed.html#code=def%20array_traversal%28nums%3A%20list%5Bint%5D%29%20-%3E%20int%3A%0A%20%20%20%20%22%22%22%E7%BA%BF%E6%80%A7%E9%98%B6%EF%BC%88%E9%81%8D%E5%8E%86%E6%95%B0%E7%BB%84%EF%BC%89%22%22%22%0A%20%20%20%20count%20%3D%200%0A%20%20%20%20%23%20%E5%BE%AA%E7%8E%AF%E6%AC%A1%E6%95%B0%E4%B8%8E%E6%95%B0%E7%BB%84%E9%95%BF%E5%BA%A6%E6%88%90%E6%AD%A3%E6%AF%94%0A%20%20%20%20for%20num%20in%20nums%3A%0A%20%20%20%20%20%20%20%20count%20%2B%3D%201%0A%20%20%20%20return%20count%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%208%0A%20%20%20%20print%28%22%E8%BE%93%E5%85%A5%E6%95%B0%E6%8D%AE%E5%A4%A7%E5%B0%8F%20n%20%3D%22,%20n%29%0A%0A%20%20%20%20count%20%3D%20array_traversal%28%5B0%5D%20*%20n%29%0A%20%20%20%20print%28%22%E7%BA%BF%E6%80%A7%E9%98%B6%EF%BC%88%E9%81%8D%E5%8E%86%E6%95%B0%E7%BB%84%EF%BC%89%E7%9A%84%E6%93%8D%E4%BD%9C%E6%95%B0%E9%87%8F%20%3D%22,%20count%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 &gt;</a></div></p>
</details>
<p>It's important to note that <strong>the input data size <span class="arithmatex">\(n\)</span> should be determined based on the type of input data</strong>. For example, in the first example, <span class="arithmatex">\(n\)</span> represents the input data size, while in the second example, the length of the array <span class="arithmatex">\(n\)</span> is the data size.</p>
<h3 id="3-quadratic-order-on2">3. &nbsp; Quadratic Order <span class="arithmatex">\(O(n^2)\)</span><a class="headerlink" href="#3-quadratic-order-on2" title="Permanent link">&para;</a></h3>
<h3 id="3-quadratic-order-on2">3. &nbsp; Quadratic order <span class="arithmatex">\(O(n^2)\)</span><a class="headerlink" href="#3-quadratic-order-on2" title="Permanent link">&para;</a></h3>
<p>Quadratic order means the number of operations grows quadratically with the input data size <span class="arithmatex">\(n\)</span>. Quadratic order typically appears in nested loops, where both the outer and inner loops have a time complexity of <span class="arithmatex">\(O(n)\)</span>, resulting in an overall complexity of <span class="arithmatex">\(O(n^2)\)</span>:</p>
<div class="tabbed-set tabbed-alternate" data-tabs="8:14"><input checked="checked" id="__tabbed_8_1" name="__tabbed_8" type="radio" /><input id="__tabbed_8_2" name="__tabbed_8" type="radio" /><input id="__tabbed_8_3" name="__tabbed_8" type="radio" /><input id="__tabbed_8_4" name="__tabbed_8" type="radio" /><input id="__tabbed_8_5" name="__tabbed_8" type="radio" /><input id="__tabbed_8_6" name="__tabbed_8" type="radio" /><input id="__tabbed_8_7" name="__tabbed_8" type="radio" /><input id="__tabbed_8_8" name="__tabbed_8" type="radio" /><input id="__tabbed_8_9" name="__tabbed_8" type="radio" /><input id="__tabbed_8_10" name="__tabbed_8" type="radio" /><input id="__tabbed_8_11" name="__tabbed_8" type="radio" /><input id="__tabbed_8_12" name="__tabbed_8" type="radio" /><input id="__tabbed_8_13" name="__tabbed_8" type="radio" /><input id="__tabbed_8_14" name="__tabbed_8" type="radio" /><div class="tabbed-labels"><label for="__tabbed_8_1">Python</label><label for="__tabbed_8_2">C++</label><label for="__tabbed_8_3">Java</label><label for="__tabbed_8_4">C#</label><label for="__tabbed_8_5">Go</label><label for="__tabbed_8_6">Swift</label><label for="__tabbed_8_7">JS</label><label for="__tabbed_8_8">TS</label><label for="__tabbed_8_9">Dart</label><label for="__tabbed_8_10">Rust</label><label for="__tabbed_8_11">C</label><label for="__tabbed_8_12">Kotlin</label><label for="__tabbed_8_13">Ruby</label><label for="__tabbed_8_14">Zig</label></div>
<div class="tabbed-content">
@@ -3900,8 +3906,8 @@ O(1) &lt; O(\log n) &lt; O(n) &lt; O(n \log n) &lt; O(n^2) &lt; O(2^n) &lt; O(n!
<div style="margin-top: 5px;"><a href="https://pythontutor.com/iframe-embed.html#code=def%20quadratic%28n%29%20-%3E%20int%3A%0A%20%20%20%20%22%22%22%E5%B9%B3%E6%96%B9%E9%98%B6%22%22%22%0A%20%20%20%20count%20%3D%200%0A%20%20%20%20%23%20%E5%BE%AA%E7%8E%AF%E6%AC%A1%E6%95%B0%E4%B8%8E%E6%95%B0%E6%8D%AE%E5%A4%A7%E5%B0%8F%20n%20%E6%88%90%E5%B9%B3%E6%96%B9%E5%85%B3%E7%B3%BB%0A%20%20%20%20for%20i%20in%20range%28n%29%3A%0A%20%20%20%20%20%20%20%20for%20j%20in%20range%28n%29%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20count%20%2B%3D%201%0A%20%20%20%20return%20count%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%208%0A%20%20%20%20print%28%22%E8%BE%93%E5%85%A5%E6%95%B0%E6%8D%AE%E5%A4%A7%E5%B0%8F%20n%20%3D%22,%20n%29%0A%0A%20%20%20%20count%20%3D%20quadratic%28n%29%0A%20%20%20%20print%28%22%E5%B9%B3%E6%96%B9%E9%98%B6%E7%9A%84%E6%93%8D%E4%BD%9C%E6%95%B0%E9%87%8F%20%3D%22,%20count%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 &gt;</a></div></p>
</details>
<p>The following image compares constant order, linear order, and quadratic order time complexities.</p>
<p><a class="glightbox" href="../time_complexity.assets/time_complexity_constant_linear_quadratic.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Constant, Linear, and Quadratic Order Time Complexities" class="animation-figure" src="../time_complexity.assets/time_complexity_constant_linear_quadratic.png" /></a></p>
<p align="center"> Figure 2-10 &nbsp; Constant, Linear, and Quadratic Order Time Complexities </p>
<p><a class="glightbox" href="../time_complexity.assets/time_complexity_constant_linear_quadratic.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Constant, linear, and quadratic order time complexities" class="animation-figure" src="../time_complexity.assets/time_complexity_constant_linear_quadratic.png" /></a></p>
<p align="center"> Figure 2-10 &nbsp; Constant, linear, and quadratic order time complexities </p>
<p>For instance, in bubble sort, the outer loop runs <span class="arithmatex">\(n - 1\)</span> times, and the inner loop runs <span class="arithmatex">\(n-1\)</span>, <span class="arithmatex">\(n-2\)</span>, ..., <span class="arithmatex">\(2\)</span>, <span class="arithmatex">\(1\)</span> times, averaging <span class="arithmatex">\(n / 2\)</span> times, resulting in a time complexity of <span class="arithmatex">\(O((n - 1) n / 2) = O(n^2)\)</span>:</p>
<div class="tabbed-set tabbed-alternate" data-tabs="9:14"><input checked="checked" id="__tabbed_9_1" name="__tabbed_9" type="radio" /><input id="__tabbed_9_2" name="__tabbed_9" type="radio" /><input id="__tabbed_9_3" name="__tabbed_9" type="radio" /><input id="__tabbed_9_4" name="__tabbed_9" type="radio" /><input id="__tabbed_9_5" name="__tabbed_9" type="radio" /><input id="__tabbed_9_6" name="__tabbed_9" type="radio" /><input id="__tabbed_9_7" name="__tabbed_9" type="radio" /><input id="__tabbed_9_8" name="__tabbed_9" type="radio" /><input id="__tabbed_9_9" name="__tabbed_9" type="radio" /><input id="__tabbed_9_10" name="__tabbed_9" type="radio" /><input id="__tabbed_9_11" name="__tabbed_9" type="radio" /><input id="__tabbed_9_12" name="__tabbed_9" type="radio" /><input id="__tabbed_9_13" name="__tabbed_9" type="radio" /><input id="__tabbed_9_14" name="__tabbed_9" type="radio" /><div class="tabbed-labels"><label for="__tabbed_9_1">Python</label><label for="__tabbed_9_2">C++</label><label for="__tabbed_9_3">Java</label><label for="__tabbed_9_4">C#</label><label for="__tabbed_9_5">Go</label><label for="__tabbed_9_6">Swift</label><label for="__tabbed_9_7">JS</label><label for="__tabbed_9_8">TS</label><label for="__tabbed_9_9">Dart</label><label for="__tabbed_9_10">Rust</label><label for="__tabbed_9_11">C</label><label for="__tabbed_9_12">Kotlin</label><label for="__tabbed_9_13">Ruby</label><label for="__tabbed_9_14">Zig</label></div>
@@ -4204,7 +4210,7 @@ O(1) &lt; O(\log n) &lt; O(n) &lt; O(n \log n) &lt; O(n^2) &lt; O(2^n) &lt; O(n!
<p><div style="height: 549px; width: 100%;"><iframe class="pythontutor-iframe" src="https://pythontutor.com/iframe-embed.html#code=def%20bubble_sort%28nums%3A%20list%5Bint%5D%29%20-%3E%20int%3A%0A%20%20%20%20%22%22%22%E5%B9%B3%E6%96%B9%E9%98%B6%EF%BC%88%E5%86%92%E6%B3%A1%E6%8E%92%E5%BA%8F%EF%BC%89%22%22%22%0A%20%20%20%20count%20%3D%200%20%20%23%20%E8%AE%A1%E6%95%B0%E5%99%A8%0A%20%20%20%20%23%20%E5%A4%96%E5%BE%AA%E7%8E%AF%EF%BC%9A%E6%9C%AA%E6%8E%92%E5%BA%8F%E5%8C%BA%E9%97%B4%E4%B8%BA%20%5B0,%20i%5D%0A%20%20%20%20for%20i%20in%20range%28len%28nums%29%20-%201,%200,%20-1%29%3A%0A%20%20%20%20%20%20%20%20%23%20%E5%86%85%E5%BE%AA%E7%8E%AF%EF%BC%9A%E5%B0%86%E6%9C%AA%E6%8E%92%E5%BA%8F%E5%8C%BA%E9%97%B4%20%5B0,%20i%5D%20%E4%B8%AD%E7%9A%84%E6%9C%80%E5%A4%A7%E5%85%83%E7%B4%A0%E4%BA%A4%E6%8D%A2%E8%87%B3%E8%AF%A5%E5%8C%BA%E9%97%B4%E7%9A%84%E6%9C%80%E5%8F%B3%E7%AB%AF%0A%20%20%20%20%20%20%20%20for%20j%20in%20range%28i%29%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20nums%5Bj%5D%20%3E%20nums%5Bj%20%2B%201%5D%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20%E4%BA%A4%E6%8D%A2%20nums%5Bj%5D%20%E4%B8%8E%20nums%5Bj%20%2B%201%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20tmp%20%3D%20nums%5Bj%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20nums%5Bj%5D%20%3D%20nums%5Bj%20%2B%201%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20nums%5Bj%20%2B%201%5D%20%3D%20tmp%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20count%20%2B%3D%203%20%20%23%20%E5%85%83%E7%B4%A0%E4%BA%A4%E6%8D%A2%E5%8C%85%E5%90%AB%203%20%E4%B8%AA%E5%8D%95%E5%85%83%E6%93%8D%E4%BD%9C%0A%20%20%20%20return%20count%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%208%0A%20%20%20%20print%28%22%E8%BE%93%E5%85%A5%E6%95%B0%E6%8D%AE%E5%A4%A7%E5%B0%8F%20n%20%3D%22,%20n%29%0A%0A%20%20%20%20nums%20%3D%20%5Bi%20for%20i%20in%20range%28n,%200,%20-1%29%5D%20%20%23%20%5Bn,%20n-1,%20...,%202,%201%5D%0A%20%20%20%20count%20%3D%20bubble_sort%28nums%29%0A%20%20%20%20print%28%22%E5%B9%B3%E6%96%B9%E9%98%B6%EF%BC%88%E5%86%92%E6%B3%A1%E6%8E%92%E5%BA%8F%EF%BC%89%E7%9A%84%E6%93%8D%E4%BD%9C%E6%95%B0%E9%87%8F%20%3D%22,%20count%29&codeDivHeight=472&codeDivWidth=350&cumulative=false&curInstr=3&heapPrimitives=nevernest&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false"> </iframe></div>
<div style="margin-top: 5px;"><a href="https://pythontutor.com/iframe-embed.html#code=def%20bubble_sort%28nums%3A%20list%5Bint%5D%29%20-%3E%20int%3A%0A%20%20%20%20%22%22%22%E5%B9%B3%E6%96%B9%E9%98%B6%EF%BC%88%E5%86%92%E6%B3%A1%E6%8E%92%E5%BA%8F%EF%BC%89%22%22%22%0A%20%20%20%20count%20%3D%200%20%20%23%20%E8%AE%A1%E6%95%B0%E5%99%A8%0A%20%20%20%20%23%20%E5%A4%96%E5%BE%AA%E7%8E%AF%EF%BC%9A%E6%9C%AA%E6%8E%92%E5%BA%8F%E5%8C%BA%E9%97%B4%E4%B8%BA%20%5B0,%20i%5D%0A%20%20%20%20for%20i%20in%20range%28len%28nums%29%20-%201,%200,%20-1%29%3A%0A%20%20%20%20%20%20%20%20%23%20%E5%86%85%E5%BE%AA%E7%8E%AF%EF%BC%9A%E5%B0%86%E6%9C%AA%E6%8E%92%E5%BA%8F%E5%8C%BA%E9%97%B4%20%5B0,%20i%5D%20%E4%B8%AD%E7%9A%84%E6%9C%80%E5%A4%A7%E5%85%83%E7%B4%A0%E4%BA%A4%E6%8D%A2%E8%87%B3%E8%AF%A5%E5%8C%BA%E9%97%B4%E7%9A%84%E6%9C%80%E5%8F%B3%E7%AB%AF%0A%20%20%20%20%20%20%20%20for%20j%20in%20range%28i%29%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20nums%5Bj%5D%20%3E%20nums%5Bj%20%2B%201%5D%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20%E4%BA%A4%E6%8D%A2%20nums%5Bj%5D%20%E4%B8%8E%20nums%5Bj%20%2B%201%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20tmp%20%3D%20nums%5Bj%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20nums%5Bj%5D%20%3D%20nums%5Bj%20%2B%201%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20nums%5Bj%20%2B%201%5D%20%3D%20tmp%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20count%20%2B%3D%203%20%20%23%20%E5%85%83%E7%B4%A0%E4%BA%A4%E6%8D%A2%E5%8C%85%E5%90%AB%203%20%E4%B8%AA%E5%8D%95%E5%85%83%E6%93%8D%E4%BD%9C%0A%20%20%20%20return%20count%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%208%0A%20%20%20%20print%28%22%E8%BE%93%E5%85%A5%E6%95%B0%E6%8D%AE%E5%A4%A7%E5%B0%8F%20n%20%3D%22,%20n%29%0A%0A%20%20%20%20nums%20%3D%20%5Bi%20for%20i%20in%20range%28n,%200,%20-1%29%5D%20%20%23%20%5Bn,%20n-1,%20...,%202,%201%5D%0A%20%20%20%20count%20%3D%20bubble_sort%28nums%29%0A%20%20%20%20print%28%22%E5%B9%B3%E6%96%B9%E9%98%B6%EF%BC%88%E5%86%92%E6%B3%A1%E6%8E%92%E5%BA%8F%EF%BC%89%E7%9A%84%E6%93%8D%E4%BD%9C%E6%95%B0%E9%87%8F%20%3D%22,%20count%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 &gt;</a></div></p>
</details>
<h3 id="4-exponential-order-o2n">4. &nbsp; Exponential Order <span class="arithmatex">\(O(2^n)\)</span><a class="headerlink" href="#4-exponential-order-o2n" title="Permanent link">&para;</a></h3>
<h3 id="4-exponential-order-o2n">4. &nbsp; Exponential order <span class="arithmatex">\(O(2^n)\)</span><a class="headerlink" href="#4-exponential-order-o2n" title="Permanent link">&para;</a></h3>
<p>Biological "cell division" is a classic example of exponential order growth: starting with one cell, it becomes two after one division, four after two divisions, and so on, resulting in <span class="arithmatex">\(2^n\)</span> cells after <span class="arithmatex">\(n\)</span> divisions.</p>
<p>The following image and code simulate the cell division process, with a time complexity of <span class="arithmatex">\(O(2^n)\)</span>:</p>
<div class="tabbed-set tabbed-alternate" data-tabs="10:14"><input checked="checked" id="__tabbed_10_1" name="__tabbed_10" type="radio" /><input id="__tabbed_10_2" name="__tabbed_10" type="radio" /><input id="__tabbed_10_3" name="__tabbed_10" type="radio" /><input id="__tabbed_10_4" name="__tabbed_10" type="radio" /><input id="__tabbed_10_5" name="__tabbed_10" type="radio" /><input id="__tabbed_10_6" name="__tabbed_10" type="radio" /><input id="__tabbed_10_7" name="__tabbed_10" type="radio" /><input id="__tabbed_10_8" name="__tabbed_10" type="radio" /><input id="__tabbed_10_9" name="__tabbed_10" type="radio" /><input id="__tabbed_10_10" name="__tabbed_10" type="radio" /><input id="__tabbed_10_11" name="__tabbed_10" type="radio" /><input id="__tabbed_10_12" name="__tabbed_10" type="radio" /><input id="__tabbed_10_13" name="__tabbed_10" type="radio" /><input id="__tabbed_10_14" name="__tabbed_10" type="radio" /><div class="tabbed-labels"><label for="__tabbed_10_1">Python</label><label for="__tabbed_10_2">C++</label><label for="__tabbed_10_3">Java</label><label for="__tabbed_10_4">C#</label><label for="__tabbed_10_5">Go</label><label for="__tabbed_10_6">Swift</label><label for="__tabbed_10_7">JS</label><label for="__tabbed_10_8">TS</label><label for="__tabbed_10_9">Dart</label><label for="__tabbed_10_10">Rust</label><label for="__tabbed_10_11">C</label><label for="__tabbed_10_12">Kotlin</label><label for="__tabbed_10_13">Ruby</label><label for="__tabbed_10_14">Zig</label></div>
@@ -4447,8 +4453,8 @@ O(1) &lt; O(\log n) &lt; O(n) &lt; O(n \log n) &lt; O(n^2) &lt; O(2^n) &lt; O(n!
<p><div style="height: 531px; width: 100%;"><iframe class="pythontutor-iframe" src="https://pythontutor.com/iframe-embed.html#code=def%20exponential%28n%29%20-%3E%20int%3A%0A%20%20%20%20%22%22%22%E6%8C%87%E6%95%B0%E9%98%B6%EF%BC%88%E5%BE%AA%E7%8E%AF%E5%AE%9E%E7%8E%B0%EF%BC%89%22%22%22%0A%20%20%20%20count%20%3D%200%0A%20%20%20%20base%20%3D%201%0A%20%20%20%20%23%20%E7%BB%86%E8%83%9E%E6%AF%8F%E8%BD%AE%E4%B8%80%E5%88%86%E4%B8%BA%E4%BA%8C%EF%BC%8C%E5%BD%A2%E6%88%90%E6%95%B0%E5%88%97%201,%202,%204,%208,%20...,%202%5E%28n-1%29%0A%20%20%20%20for%20_%20in%20range%28n%29%3A%0A%20%20%20%20%20%20%20%20for%20_%20in%20range%28base%29%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20count%20%2B%3D%201%0A%20%20%20%20%20%20%20%20base%20*%3D%202%0A%20%20%20%20%23%20count%20%3D%201%20%2B%202%20%2B%204%20%2B%208%20%2B%20..%20%2B%202%5E%28n-1%29%20%3D%202%5En%20-%201%0A%20%20%20%20return%20count%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%208%0A%20%20%20%20print%28%22%E8%BE%93%E5%85%A5%E6%95%B0%E6%8D%AE%E5%A4%A7%E5%B0%8F%20n%20%3D%22,%20n%29%0A%0A%20%20%20%20count%20%3D%20exponential%28n%29%0A%20%20%20%20print%28%22%E6%8C%87%E6%95%B0%E9%98%B6%EF%BC%88%E5%BE%AA%E7%8E%AF%E5%AE%9E%E7%8E%B0%EF%BC%89%E7%9A%84%E6%93%8D%E4%BD%9C%E6%95%B0%E9%87%8F%20%3D%22,%20count%29&codeDivHeight=472&codeDivWidth=350&cumulative=false&curInstr=3&heapPrimitives=nevernest&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false"> </iframe></div>
<div style="margin-top: 5px;"><a href="https://pythontutor.com/iframe-embed.html#code=def%20exponential%28n%29%20-%3E%20int%3A%0A%20%20%20%20%22%22%22%E6%8C%87%E6%95%B0%E9%98%B6%EF%BC%88%E5%BE%AA%E7%8E%AF%E5%AE%9E%E7%8E%B0%EF%BC%89%22%22%22%0A%20%20%20%20count%20%3D%200%0A%20%20%20%20base%20%3D%201%0A%20%20%20%20%23%20%E7%BB%86%E8%83%9E%E6%AF%8F%E8%BD%AE%E4%B8%80%E5%88%86%E4%B8%BA%E4%BA%8C%EF%BC%8C%E5%BD%A2%E6%88%90%E6%95%B0%E5%88%97%201,%202,%204,%208,%20...,%202%5E%28n-1%29%0A%20%20%20%20for%20_%20in%20range%28n%29%3A%0A%20%20%20%20%20%20%20%20for%20_%20in%20range%28base%29%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20count%20%2B%3D%201%0A%20%20%20%20%20%20%20%20base%20*%3D%202%0A%20%20%20%20%23%20count%20%3D%201%20%2B%202%20%2B%204%20%2B%208%20%2B%20..%20%2B%202%5E%28n-1%29%20%3D%202%5En%20-%201%0A%20%20%20%20return%20count%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%208%0A%20%20%20%20print%28%22%E8%BE%93%E5%85%A5%E6%95%B0%E6%8D%AE%E5%A4%A7%E5%B0%8F%20n%20%3D%22,%20n%29%0A%0A%20%20%20%20count%20%3D%20exponential%28n%29%0A%20%20%20%20print%28%22%E6%8C%87%E6%95%B0%E9%98%B6%EF%BC%88%E5%BE%AA%E7%8E%AF%E5%AE%9E%E7%8E%B0%EF%BC%89%E7%9A%84%E6%93%8D%E4%BD%9C%E6%95%B0%E9%87%8F%20%3D%22,%20count%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 &gt;</a></div></p>
</details>
<p><a class="glightbox" href="../time_complexity.assets/time_complexity_exponential.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Exponential Order Time Complexity" class="animation-figure" src="../time_complexity.assets/time_complexity_exponential.png" /></a></p>
<p align="center"> Figure 2-11 &nbsp; Exponential Order Time Complexity </p>
<p><a class="glightbox" href="../time_complexity.assets/time_complexity_exponential.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Exponential order time complexity" class="animation-figure" src="../time_complexity.assets/time_complexity_exponential.png" /></a></p>
<p align="center"> Figure 2-11 &nbsp; Exponential order time complexity </p>
<p>In practice, exponential order often appears in recursive functions. For example, in the code below, it recursively splits into two halves, stopping after <span class="arithmatex">\(n\)</span> divisions:</p>
<div class="tabbed-set tabbed-alternate" data-tabs="11:14"><input checked="checked" id="__tabbed_11_1" name="__tabbed_11" type="radio" /><input id="__tabbed_11_2" name="__tabbed_11" type="radio" /><input id="__tabbed_11_3" name="__tabbed_11" type="radio" /><input id="__tabbed_11_4" name="__tabbed_11" type="radio" /><input id="__tabbed_11_5" name="__tabbed_11" type="radio" /><input id="__tabbed_11_6" name="__tabbed_11" type="radio" /><input id="__tabbed_11_7" name="__tabbed_11" type="radio" /><input id="__tabbed_11_8" name="__tabbed_11" type="radio" /><input id="__tabbed_11_9" name="__tabbed_11" type="radio" /><input id="__tabbed_11_10" name="__tabbed_11" type="radio" /><input id="__tabbed_11_11" name="__tabbed_11" type="radio" /><input id="__tabbed_11_12" name="__tabbed_11" type="radio" /><input id="__tabbed_11_13" name="__tabbed_11" type="radio" /><input id="__tabbed_11_14" name="__tabbed_11" type="radio" /><div class="tabbed-labels"><label for="__tabbed_11_1">Python</label><label for="__tabbed_11_2">C++</label><label for="__tabbed_11_3">Java</label><label for="__tabbed_11_4">C#</label><label for="__tabbed_11_5">Go</label><label for="__tabbed_11_6">Swift</label><label for="__tabbed_11_7">JS</label><label for="__tabbed_11_8">TS</label><label for="__tabbed_11_9">Dart</label><label for="__tabbed_11_10">Rust</label><label for="__tabbed_11_11">C</label><label for="__tabbed_11_12">Kotlin</label><label for="__tabbed_11_13">Ruby</label><label for="__tabbed_11_14">Zig</label></div>
@@ -4584,7 +4590,7 @@ O(1) &lt; O(\log n) &lt; O(n) &lt; O(n \log n) &lt; O(n^2) &lt; O(2^n) &lt; O(n!
<div style="margin-top: 5px;"><a href="https://pythontutor.com/iframe-embed.html#code=def%20exp_recur%28n%29%20-%3E%20int%3A%0A%20%20%20%20%22%22%22%E6%8C%87%E6%95%B0%E9%98%B6%EF%BC%88%E9%80%92%E5%BD%92%E5%AE%9E%E7%8E%B0%EF%BC%89%22%22%22%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%20return%20exp_recur%28n%20-%201%29%20%2B%20exp_recur%28n%20-%201%29%20%2B%201%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%207%0A%20%20%20%20print%28%22%E8%BE%93%E5%85%A5%E6%95%B0%E6%8D%AE%E5%A4%A7%E5%B0%8F%20n%20%3D%22,%20n%29%0A%0A%20%20%20%20count%20%3D%20exp_recur%28n%29%0A%20%20%20%20print%28%22%E6%8C%87%E6%95%B0%E9%98%B6%EF%BC%88%E9%80%92%E5%BD%92%E5%AE%9E%E7%8E%B0%EF%BC%89%E7%9A%84%E6%93%8D%E4%BD%9C%E6%95%B0%E9%87%8F%20%3D%22,%20count%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 &gt;</a></div></p>
</details>
<p>Exponential order growth is extremely rapid and is commonly seen in exhaustive search methods (brute force, backtracking, etc.). For large-scale problems, exponential order is unacceptable, often requiring dynamic programming or greedy algorithms as solutions.</p>
<h3 id="5-logarithmic-order-olog-n">5. &nbsp; Logarithmic Order <span class="arithmatex">\(O(\log n)\)</span><a class="headerlink" href="#5-logarithmic-order-olog-n" title="Permanent link">&para;</a></h3>
<h3 id="5-logarithmic-order-olog-n">5. &nbsp; Logarithmic order <span class="arithmatex">\(O(\log n)\)</span><a class="headerlink" href="#5-logarithmic-order-olog-n" title="Permanent link">&para;</a></h3>
<p>In contrast to exponential order, logarithmic order reflects situations where "the size is halved each round." Given an input data size <span class="arithmatex">\(n\)</span>, since the size is halved each round, the number of iterations is <span class="arithmatex">\(\log_2 n\)</span>, the inverse function of <span class="arithmatex">\(2^n\)</span>.</p>
<p>The following image and code simulate the "halving each round" process, with a time complexity of <span class="arithmatex">\(O(\log_2 n)\)</span>, commonly abbreviated as <span class="arithmatex">\(O(\log n)\)</span>:</p>
<div class="tabbed-set tabbed-alternate" data-tabs="12:14"><input checked="checked" id="__tabbed_12_1" name="__tabbed_12" type="radio" /><input id="__tabbed_12_2" name="__tabbed_12" type="radio" /><input id="__tabbed_12_3" name="__tabbed_12" type="radio" /><input id="__tabbed_12_4" name="__tabbed_12" type="radio" /><input id="__tabbed_12_5" name="__tabbed_12" type="radio" /><input id="__tabbed_12_6" name="__tabbed_12" type="radio" /><input id="__tabbed_12_7" name="__tabbed_12" type="radio" /><input id="__tabbed_12_8" name="__tabbed_12" type="radio" /><input id="__tabbed_12_9" name="__tabbed_12" type="radio" /><input id="__tabbed_12_10" name="__tabbed_12" type="radio" /><input id="__tabbed_12_11" name="__tabbed_12" type="radio" /><input id="__tabbed_12_12" name="__tabbed_12" type="radio" /><input id="__tabbed_12_13" name="__tabbed_12" type="radio" /><input id="__tabbed_12_14" name="__tabbed_12" type="radio" /><div class="tabbed-labels"><label for="__tabbed_12_1">Python</label><label for="__tabbed_12_2">C++</label><label for="__tabbed_12_3">Java</label><label for="__tabbed_12_4">C#</label><label for="__tabbed_12_5">Go</label><label for="__tabbed_12_6">Swift</label><label for="__tabbed_12_7">JS</label><label for="__tabbed_12_8">TS</label><label for="__tabbed_12_9">Dart</label><label for="__tabbed_12_10">Rust</label><label for="__tabbed_12_11">C</label><label for="__tabbed_12_12">Kotlin</label><label for="__tabbed_12_13">Ruby</label><label for="__tabbed_12_14">Zig</label></div>
@@ -4768,8 +4774,8 @@ O(1) &lt; O(\log n) &lt; O(n) &lt; O(n \log n) &lt; O(n^2) &lt; O(2^n) &lt; O(n!
<p><div style="height: 459px; width: 100%;"><iframe class="pythontutor-iframe" src="https://pythontutor.com/iframe-embed.html#code=def%20logarithmic%28n%3A%20int%29%20-%3E%20int%3A%0A%20%20%20%20%22%22%22%E5%AF%B9%E6%95%B0%E9%98%B6%EF%BC%88%E5%BE%AA%E7%8E%AF%E5%AE%9E%E7%8E%B0%EF%BC%89%22%22%22%0A%20%20%20%20count%20%3D%200%0A%20%20%20%20while%20n%20%3E%201%3A%0A%20%20%20%20%20%20%20%20n%20%3D%20n%20/%202%0A%20%20%20%20%20%20%20%20count%20%2B%3D%201%0A%20%20%20%20return%20count%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%208%0A%20%20%20%20print%28%22%E8%BE%93%E5%85%A5%E6%95%B0%E6%8D%AE%E5%A4%A7%E5%B0%8F%20n%20%3D%22,%20n%29%0A%0A%20%20%20%20count%20%3D%20logarithmic%28n%29%0A%20%20%20%20print%28%22%E5%AF%B9%E6%95%B0%E9%98%B6%EF%BC%88%E5%BE%AA%E7%8E%AF%E5%AE%9E%E7%8E%B0%EF%BC%89%E7%9A%84%E6%93%8D%E4%BD%9C%E6%95%B0%E9%87%8F%20%3D%22,%20count%29&codeDivHeight=472&codeDivWidth=350&cumulative=false&curInstr=3&heapPrimitives=nevernest&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false"> </iframe></div>
<div style="margin-top: 5px;"><a href="https://pythontutor.com/iframe-embed.html#code=def%20logarithmic%28n%3A%20int%29%20-%3E%20int%3A%0A%20%20%20%20%22%22%22%E5%AF%B9%E6%95%B0%E9%98%B6%EF%BC%88%E5%BE%AA%E7%8E%AF%E5%AE%9E%E7%8E%B0%EF%BC%89%22%22%22%0A%20%20%20%20count%20%3D%200%0A%20%20%20%20while%20n%20%3E%201%3A%0A%20%20%20%20%20%20%20%20n%20%3D%20n%20/%202%0A%20%20%20%20%20%20%20%20count%20%2B%3D%201%0A%20%20%20%20return%20count%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%208%0A%20%20%20%20print%28%22%E8%BE%93%E5%85%A5%E6%95%B0%E6%8D%AE%E5%A4%A7%E5%B0%8F%20n%20%3D%22,%20n%29%0A%0A%20%20%20%20count%20%3D%20logarithmic%28n%29%0A%20%20%20%20print%28%22%E5%AF%B9%E6%95%B0%E9%98%B6%EF%BC%88%E5%BE%AA%E7%8E%AF%E5%AE%9E%E7%8E%B0%EF%BC%89%E7%9A%84%E6%93%8D%E4%BD%9C%E6%95%B0%E9%87%8F%20%3D%22,%20count%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 &gt;</a></div></p>
</details>
<p><a class="glightbox" href="../time_complexity.assets/time_complexity_logarithmic.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Logarithmic Order Time Complexity" class="animation-figure" src="../time_complexity.assets/time_complexity_logarithmic.png" /></a></p>
<p align="center"> Figure 2-12 &nbsp; Logarithmic Order Time Complexity </p>
<p><a class="glightbox" href="../time_complexity.assets/time_complexity_logarithmic.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Logarithmic order time complexity" class="animation-figure" src="../time_complexity.assets/time_complexity_logarithmic.png" /></a></p>
<p align="center"> Figure 2-12 &nbsp; Logarithmic order time complexity </p>
<p>Like exponential order, logarithmic order also frequently appears in recursive functions. The code below forms a recursive tree of height <span class="arithmatex">\(\log_2 n\)</span>:</p>
<div class="tabbed-set tabbed-alternate" data-tabs="13:14"><input checked="checked" id="__tabbed_13_1" name="__tabbed_13" type="radio" /><input id="__tabbed_13_2" name="__tabbed_13" type="radio" /><input id="__tabbed_13_3" name="__tabbed_13" type="radio" /><input id="__tabbed_13_4" name="__tabbed_13" type="radio" /><input id="__tabbed_13_5" name="__tabbed_13" type="radio" /><input id="__tabbed_13_6" name="__tabbed_13" type="radio" /><input id="__tabbed_13_7" name="__tabbed_13" type="radio" /><input id="__tabbed_13_8" name="__tabbed_13" type="radio" /><input id="__tabbed_13_9" name="__tabbed_13" type="radio" /><input id="__tabbed_13_10" name="__tabbed_13" type="radio" /><input id="__tabbed_13_11" name="__tabbed_13" type="radio" /><input id="__tabbed_13_12" name="__tabbed_13" type="radio" /><input id="__tabbed_13_13" name="__tabbed_13" type="radio" /><input id="__tabbed_13_14" name="__tabbed_13" type="radio" /><div class="tabbed-labels"><label for="__tabbed_13_1">Python</label><label for="__tabbed_13_2">C++</label><label for="__tabbed_13_3">Java</label><label for="__tabbed_13_4">C#</label><label for="__tabbed_13_5">Go</label><label for="__tabbed_13_6">Swift</label><label for="__tabbed_13_7">JS</label><label for="__tabbed_13_8">TS</label><label for="__tabbed_13_9">Dart</label><label for="__tabbed_13_10">Rust</label><label for="__tabbed_13_11">C</label><label for="__tabbed_13_12">Kotlin</label><label for="__tabbed_13_13">Ruby</label><label for="__tabbed_13_14">Zig</label></div>
@@ -4912,7 +4918,7 @@ O(\log_m n) = O(\log_k n / \log_k m) = O(\log_k n)
\]</div>
<p>This means the base <span class="arithmatex">\(m\)</span> can be changed without affecting the complexity. Therefore, we often omit the base <span class="arithmatex">\(m\)</span> and simply denote logarithmic order as <span class="arithmatex">\(O(\log n)\)</span>.</p>
</div>
<h3 id="6-linear-logarithmic-order-on-log-n">6. &nbsp; Linear-Logarithmic Order <span class="arithmatex">\(O(n \log n)\)</span><a class="headerlink" href="#6-linear-logarithmic-order-on-log-n" title="Permanent link">&para;</a></h3>
<h3 id="6-linear-logarithmic-order-on-log-n">6. &nbsp; Linear-logarithmic order <span class="arithmatex">\(O(n \log n)\)</span><a class="headerlink" href="#6-linear-logarithmic-order-on-log-n" title="Permanent link">&para;</a></h3>
<p>Linear-logarithmic order often appears in nested loops, with the complexities of the two loops being <span class="arithmatex">\(O(\log n)\)</span> and <span class="arithmatex">\(O(n)\)</span> respectively. The related code is as follows:</p>
<div class="tabbed-set tabbed-alternate" data-tabs="14:14"><input checked="checked" id="__tabbed_14_1" name="__tabbed_14" type="radio" /><input id="__tabbed_14_2" name="__tabbed_14" type="radio" /><input id="__tabbed_14_3" name="__tabbed_14" type="radio" /><input id="__tabbed_14_4" name="__tabbed_14" type="radio" /><input id="__tabbed_14_5" name="__tabbed_14" type="radio" /><input id="__tabbed_14_6" name="__tabbed_14" type="radio" /><input id="__tabbed_14_7" name="__tabbed_14" type="radio" /><input id="__tabbed_14_8" name="__tabbed_14" type="radio" /><input id="__tabbed_14_9" name="__tabbed_14" type="radio" /><input id="__tabbed_14_10" name="__tabbed_14" type="radio" /><input id="__tabbed_14_11" name="__tabbed_14" type="radio" /><input id="__tabbed_14_12" name="__tabbed_14" type="radio" /><input id="__tabbed_14_13" name="__tabbed_14" type="radio" /><input id="__tabbed_14_14" name="__tabbed_14" type="radio" /><div class="tabbed-labels"><label for="__tabbed_14_1">Python</label><label for="__tabbed_14_2">C++</label><label for="__tabbed_14_3">Java</label><label for="__tabbed_14_4">C#</label><label for="__tabbed_14_5">Go</label><label for="__tabbed_14_6">Swift</label><label for="__tabbed_14_7">JS</label><label for="__tabbed_14_8">TS</label><label for="__tabbed_14_9">Dart</label><label for="__tabbed_14_10">Rust</label><label for="__tabbed_14_11">C</label><label for="__tabbed_14_12">Kotlin</label><label for="__tabbed_14_13">Ruby</label><label for="__tabbed_14_14">Zig</label></div>
<div class="tabbed-content">
@@ -5102,11 +5108,11 @@ O(\log_m n) = O(\log_k n / \log_k m) = O(\log_k n)
<div style="margin-top: 5px;"><a href="https://pythontutor.com/iframe-embed.html#code=def%20linear_log_recur%28n%3A%20int%29%20-%3E%20int%3A%0A%20%20%20%20%22%22%22%E7%BA%BF%E6%80%A7%E5%AF%B9%E6%95%B0%E9%98%B6%22%22%22%0A%20%20%20%20if%20n%20%3C%3D%201%3A%0A%20%20%20%20%20%20%20%20return%201%0A%20%20%20%20count%20%3D%20linear_log_recur%28n%20//%202%29%20%2B%20linear_log_recur%28n%20//%202%29%0A%20%20%20%20for%20_%20in%20range%28n%29%3A%0A%20%20%20%20%20%20%20%20count%20%2B%3D%201%0A%20%20%20%20return%20count%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%208%0A%20%20%20%20print%28%22%E8%BE%93%E5%85%A5%E6%95%B0%E6%8D%AE%E5%A4%A7%E5%B0%8F%20n%20%3D%22,%20n%29%0A%0A%20%20%20%20count%20%3D%20linear_log_recur%28n%29%0A%20%20%20%20print%28%22%E7%BA%BF%E6%80%A7%E5%AF%B9%E6%95%B0%E9%98%B6%EF%BC%88%E9%80%92%E5%BD%92%E5%AE%9E%E7%8E%B0%EF%BC%89%E7%9A%84%E6%93%8D%E4%BD%9C%E6%95%B0%E9%87%8F%20%3D%22,%20count%29&codeDivHeight=800&codeDivWidth=600&cumulative=false&curInstr=4&heapPrimitives=nevernest&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false" target="_blank" rel="noopener noreferrer">Full Screen &gt;</a></div></p>
</details>
<p>The image below demonstrates how linear-logarithmic order is generated. Each level of a binary tree has <span class="arithmatex">\(n\)</span> operations, and the tree has <span class="arithmatex">\(\log_2 n + 1\)</span> levels, resulting in a time complexity of <span class="arithmatex">\(O(n \log n)\)</span>.</p>
<p><a class="glightbox" href="../time_complexity.assets/time_complexity_logarithmic_linear.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Linear-Logarithmic Order Time Complexity" class="animation-figure" src="../time_complexity.assets/time_complexity_logarithmic_linear.png" /></a></p>
<p align="center"> Figure 2-13 &nbsp; Linear-Logarithmic Order Time Complexity </p>
<p><a class="glightbox" href="../time_complexity.assets/time_complexity_logarithmic_linear.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Linear-logarithmic order time complexity" class="animation-figure" src="../time_complexity.assets/time_complexity_logarithmic_linear.png" /></a></p>
<p align="center"> Figure 2-13 &nbsp; Linear-logarithmic order time complexity </p>
<p>Mainstream sorting algorithms typically have a time complexity of <span class="arithmatex">\(O(n \log n)\)</span>, such as quicksort, mergesort, and heapsort.</p>
<h3 id="7-factorial-order-on">7. &nbsp; Factorial Order <span class="arithmatex">\(O(n!)\)</span><a class="headerlink" href="#7-factorial-order-on" title="Permanent link">&para;</a></h3>
<h3 id="7-factorial-order-on">7. &nbsp; Factorial order <span class="arithmatex">\(O(n!)\)</span><a class="headerlink" href="#7-factorial-order-on" title="Permanent link">&para;</a></h3>
<p>Factorial order corresponds to the mathematical problem of "full permutation." Given <span class="arithmatex">\(n\)</span> distinct elements, the total number of possible permutations is:</p>
<div class="arithmatex">\[
n! = n \times (n - 1) \times (n - 2) \times \dots \times 2 \times 1
@@ -5312,11 +5318,11 @@ n! = n \times (n - 1) \times (n - 2) \times \dots \times 2 \times 1
<p><div style="height: 495px; width: 100%;"><iframe class="pythontutor-iframe" src="https://pythontutor.com/iframe-embed.html#code=def%20factorial_recur%28n%29%20-%3E%20int%3A%0A%20%20%20%20%22%22%22%E9%98%B6%E4%B9%98%E9%98%B6%EF%BC%88%E9%80%92%E5%BD%92%E5%AE%9E%E7%8E%B0%EF%BC%89%22%22%22%0A%20%20%20%20if%20n%20%3D%3D%200%3A%0A%20%20%20%20%20%20%20%20return%201%0A%20%20%20%20count%20%3D%200%0A%20%20%20%20%23%20%E4%BB%8E%201%20%E4%B8%AA%E5%88%86%E8%A3%82%E5%87%BA%20n%20%E4%B8%AA%0A%20%20%20%20for%20_%20in%20range%28n%29%3A%0A%20%20%20%20%20%20%20%20count%20%2B%3D%20factorial_recur%28n%20-%201%29%0A%20%20%20%20return%20count%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%204%0A%20%20%20%20print%28%22%E8%BE%93%E5%85%A5%E6%95%B0%E6%8D%AE%E5%A4%A7%E5%B0%8F%20n%20%3D%22,%20n%29%0A%0A%20%20%20%20count%20%3D%20factorial_recur%28n%29%0A%20%20%20%20print%28%22%E9%98%B6%E4%B9%98%E9%98%B6%EF%BC%88%E9%80%92%E5%BD%92%E5%AE%9E%E7%8E%B0%EF%BC%89%E7%9A%84%E6%93%8D%E4%BD%9C%E6%95%B0%E9%87%8F%20%3D%22,%20count%29&codeDivHeight=472&codeDivWidth=350&cumulative=false&curInstr=3&heapPrimitives=nevernest&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false"> </iframe></div>
<div style="margin-top: 5px;"><a href="https://pythontutor.com/iframe-embed.html#code=def%20factorial_recur%28n%29%20-%3E%20int%3A%0A%20%20%20%20%22%22%22%E9%98%B6%E4%B9%98%E9%98%B6%EF%BC%88%E9%80%92%E5%BD%92%E5%AE%9E%E7%8E%B0%EF%BC%89%22%22%22%0A%20%20%20%20if%20n%20%3D%3D%200%3A%0A%20%20%20%20%20%20%20%20return%201%0A%20%20%20%20count%20%3D%200%0A%20%20%20%20%23%20%E4%BB%8E%201%20%E4%B8%AA%E5%88%86%E8%A3%82%E5%87%BA%20n%20%E4%B8%AA%0A%20%20%20%20for%20_%20in%20range%28n%29%3A%0A%20%20%20%20%20%20%20%20count%20%2B%3D%20factorial_recur%28n%20-%201%29%0A%20%20%20%20return%20count%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%204%0A%20%20%20%20print%28%22%E8%BE%93%E5%85%A5%E6%95%B0%E6%8D%AE%E5%A4%A7%E5%B0%8F%20n%20%3D%22,%20n%29%0A%0A%20%20%20%20count%20%3D%20factorial_recur%28n%29%0A%20%20%20%20print%28%22%E9%98%B6%E4%B9%98%E9%98%B6%EF%BC%88%E9%80%92%E5%BD%92%E5%AE%9E%E7%8E%B0%EF%BC%89%E7%9A%84%E6%93%8D%E4%BD%9C%E6%95%B0%E9%87%8F%20%3D%22,%20count%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 &gt;</a></div></p>
</details>
<p><a class="glightbox" href="../time_complexity.assets/time_complexity_factorial.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Factorial Order Time Complexity" class="animation-figure" src="../time_complexity.assets/time_complexity_factorial.png" /></a></p>
<p align="center"> Figure 2-14 &nbsp; Factorial Order Time Complexity </p>
<p><a class="glightbox" href="../time_complexity.assets/time_complexity_factorial.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Factorial order time complexity" class="animation-figure" src="../time_complexity.assets/time_complexity_factorial.png" /></a></p>
<p align="center"> Figure 2-14 &nbsp; Factorial order time complexity </p>
<p>Note that factorial order grows even faster than exponential order; it's unacceptable for larger <span class="arithmatex">\(n\)</span> values.</p>
<h2 id="235-worst-best-and-average-time-complexities">2.3.5 &nbsp; Worst, Best, and Average Time Complexities<a class="headerlink" href="#235-worst-best-and-average-time-complexities" title="Permanent link">&para;</a></h2>
<h2 id="235-worst-best-and-average-time-complexities">2.3.5 &nbsp; Worst, best, and average time complexities<a class="headerlink" href="#235-worst-best-and-average-time-complexities" title="Permanent link">&para;</a></h2>
<p><strong>The time efficiency of an algorithm is often not fixed but depends on the distribution of the input data</strong>. Assume we have an array <code>nums</code> of length <span class="arithmatex">\(n\)</span>, consisting of numbers from <span class="arithmatex">\(1\)</span> to <span class="arithmatex">\(n\)</span>, each appearing only once, but in a randomly shuffled order. The task is to return the index of the element <span class="arithmatex">\(1\)</span>. We can draw the following conclusions:</p>
<ul>
<li>When <code>nums = [?, ?, ..., 1]</code>, that is, when the last element is <span class="arithmatex">\(1\)</span>, it requires a complete traversal of the array, <strong>achieving the worst-case time complexity of <span class="arithmatex">\(O(n)\)</span></strong>.</li>
@@ -5737,7 +5743,7 @@ aria-label="Footer"
<a
href="../iteration_and_recursion/"
class="md-footer__link md-footer__link--prev"
aria-label="Previous: 2.2 Iteration and Recursion"
aria-label="Previous: 2.2 Iteration and recursion"
rel="prev"
>
<div class="md-footer__button md-icon">
@@ -5749,7 +5755,7 @@ aria-label="Footer"
Previous
</span>
<div class="md-ellipsis">
2.2 Iteration and Recursion
2.2 Iteration and recursion
</div>
</div>
</a>
@@ -5761,7 +5767,7 @@ aria-label="Footer"
<a
href="../space_complexity/"
class="md-footer__link md-footer__link--next"
aria-label="Next: 2.4 Space Complexity"
aria-label="Next: 2.4 Space complexity"
rel="next"
>
<div class="md-footer__title">
@@ -5769,7 +5775,7 @@ aria-label="Footer"
Next
</span>
<div class="md-ellipsis">
2.4 Space Complexity
2.4 Space complexity
</div>
</div>
<div class="md-footer__button md-icon">
@@ -5862,7 +5868,7 @@ aria-label="Footer"
<nav class="md-footer__inner md-grid" aria-label="Footer" >
<a href="../iteration_and_recursion/" class="md-footer__link md-footer__link--prev" aria-label="Previous: 2.2 Iteration and Recursion">
<a href="../iteration_and_recursion/" class="md-footer__link md-footer__link--prev" aria-label="Previous: 2.2 Iteration and recursion">
<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>
@@ -5872,20 +5878,20 @@ aria-label="Footer"
Previous
</span>
<div class="md-ellipsis">
2.2 Iteration and Recursion
2.2 Iteration and recursion
</div>
</div>
</a>
<a href="../space_complexity/" class="md-footer__link md-footer__link--next" aria-label="Next: 2.4 Space Complexity">
<a href="../space_complexity/" class="md-footer__link md-footer__link--next" aria-label="Next: 2.4 Space complexity">
<div class="md-footer__title">
<span class="md-footer__direction">
Next
</span>
<div class="md-ellipsis">
2.4 Space Complexity
2.4 Space complexity
</div>
</div>
<div class="md-footer__button md-icon">