This commit is contained in:
krahets
2024-04-06 03:02:26 +08:00
parent 5988d20958
commit a263b839b9
875 changed files with 484736 additions and 2893 deletions
@@ -26,7 +26,7 @@
<title>2.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">