This commit is contained in:
krahets
2025-03-14 17:51:07 +08:00
parent 1b1e1e354a
commit 3b08169043
39 changed files with 956 additions and 958 deletions
+12 -12
View File
@@ -3602,8 +3602,8 @@
<!-- Page content -->
<h1 id="113-bubble-sort">11.3 &nbsp; Bubble sort<a class="headerlink" href="#113-bubble-sort" title="Permanent link">&para;</a></h1>
<p><u>Bubble sort</u> achieves sorting by continuously comparing and swapping adjacent elements. This process resembles bubbles rising from the bottom to the top, hence the name bubble sort.</p>
<p>As shown in Figure 11-4, the bubbling process can be simulated using element swap operations: starting from the leftmost end of the array and moving right, sequentially compare the size of adjacent elements. If "left element &gt; right element," then swap them. After the traversal, the largest element will be moved to the far right end of the array.</p>
<p><u>Bubble sort</u> works by continuously comparing and swapping adjacent elements. This process is like bubbles rising from the bottom to the top, hence the name "bubble sort."</p>
<p>As shown in Figure 11-4, the bubbling process can be simulated using element swaps: start from the leftmost end of the array and move right, comparing each pair of adjacent elements. If the left element is greater than the right element, swap them. After the traversal, the largest element will have bubbled up to the rightmost end of the array.</p>
<div class="tabbed-set tabbed-alternate" data-tabs="1:7"><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" /><div class="tabbed-labels"><label for="__tabbed_1_1">&lt;1&gt;</label><label for="__tabbed_1_2">&lt;2&gt;</label><label for="__tabbed_1_3">&lt;3&gt;</label><label for="__tabbed_1_4">&lt;4&gt;</label><label for="__tabbed_1_5">&lt;5&gt;</label><label for="__tabbed_1_6">&lt;6&gt;</label><label for="__tabbed_1_7">&lt;7&gt;</label></div>
<div class="tabbed-content">
<div class="tabbed-block">
@@ -3632,12 +3632,12 @@
<p align="center"> Figure 11-4 &nbsp; Simulating bubble process using element swap </p>
<h2 id="1131-algorithm-process">11.3.1 &nbsp; Algorithm process<a class="headerlink" href="#1131-algorithm-process" title="Permanent link">&para;</a></h2>
<p>Assuming the length of the array is <span class="arithmatex">\(n\)</span>, the steps of bubble sort are shown in Figure 11-5.</p>
<p>Assume the array has length <span class="arithmatex">\(n\)</span>. The steps of bubble sort are shown in Figure 11-5:</p>
<ol>
<li>First, perform a "bubble" on <span class="arithmatex">\(n\)</span> elements, <strong>swapping the largest element to its correct position</strong>.</li>
<li>Next, perform a "bubble" on the remaining <span class="arithmatex">\(n - 1\)</span> elements, <strong>swapping the second largest element to its correct position</strong>.</li>
<li>Similarly, after <span class="arithmatex">\(n - 1\)</span> rounds of "bubbling," <strong>the top <span class="arithmatex">\(n - 1\)</span> largest elements will be swapped to their correct positions</strong>.</li>
<li>The only remaining element is necessarily the smallest and does not require sorting, thus the array sorting is complete.</li>
<li>First, perform one "bubble" pass on <span class="arithmatex">\(n\)</span> elements, <strong>swapping the largest element to its correct position</strong>.</li>
<li>Next, perform a "bubble" pass on the remaining <span class="arithmatex">\(n - 1\)</span> elements, <strong>swapping the second largest element to its correct position</strong>.</li>
<li>Continue in this manner; after <span class="arithmatex">\(n - 1\)</span> such passes, <strong>the largest <span class="arithmatex">\(n - 1\)</span> elements will have been moved to their correct positions</strong>.</li>
<li>The only remaining element <strong>must</strong> be the smallest, so <strong>no</strong> further sorting is required. At this point, the array is sorted.</li>
</ol>
<p><a class="glightbox" href="../bubble_sort.assets/bubble_sort_overview.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Bubble sort process" class="animation-figure" src="../bubble_sort.assets/bubble_sort_overview.png" /></a></p>
<p align="center"> Figure 11-5 &nbsp; Bubble sort process </p>
@@ -3740,8 +3740,8 @@
</div>
</div>
<h2 id="1132-efficiency-optimization">11.3.2 &nbsp; Efficiency optimization<a class="headerlink" href="#1132-efficiency-optimization" title="Permanent link">&para;</a></h2>
<p>We find that if no swaps are performed in a round of "bubbling," the array is already sorted, and we can return the result immediately. Thus, we can add a flag <code>flag</code> to monitor this situation and return immediately when it occurs.</p>
<p>Even after optimization, the worst-case time complexity and average time complexity of bubble sort remain at <span class="arithmatex">\(O(n^2)\)</span>; however, when the input array is completely ordered, it can achieve the best time complexity of <span class="arithmatex">\(O(n)\)</span>.</p>
<p>If no swaps occur during a round of "bubbling," the array is already sorted, so we can return immediately. To detect this, we can add a <code>flag</code> variable; whenever no swaps are made in a pass, we set the flag and return early.</p>
<p>Even with this optimization, the worst time complexity and average time complexity of bubble sort remains <span class="arithmatex">\(O(n^2)\)</span>. However, if the input array is already sorted, the best-case time complexity can be as low as <span class="arithmatex">\(O(n)\)</span>.</p>
<div class="tabbed-set tabbed-alternate" data-tabs="3:14"><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" /><input id="__tabbed_3_14" 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">Ruby</label><label for="__tabbed_3_14">Zig</label></div>
<div class="tabbed-content">
<div class="tabbed-block">
@@ -3852,9 +3852,9 @@
</div>
<h2 id="1133-algorithm-characteristics">11.3.3 &nbsp; Algorithm characteristics<a class="headerlink" href="#1133-algorithm-characteristics" title="Permanent link">&para;</a></h2>
<ul>
<li><strong>Time complexity of <span class="arithmatex">\(O(n^2)\)</span>, adaptive sorting</strong>: The length of the array traversed in each round of "bubbling" decreases sequentially from <span class="arithmatex">\(n - 1\)</span>, <span class="arithmatex">\(n - 2\)</span>, <span class="arithmatex">\(\dots\)</span>, <span class="arithmatex">\(2\)</span>, <span class="arithmatex">\(1\)</span>, totaling <span class="arithmatex">\((n - 1) n / 2\)</span>. With the introduction of <code>flag</code> optimization, the best time complexity can reach <span class="arithmatex">\(O(n)\)</span>.</li>
<li><strong>Space complexity of <span class="arithmatex">\(O(1)\)</span>, in-place sorting</strong>: Only a constant amount of extra space is used by pointers <span class="arithmatex">\(i\)</span> and <span class="arithmatex">\(j\)</span>.</li>
<li><strong>Stable sorting</strong>: As equal elements are not swapped during the "bubbling".</li>
<li><strong>Time complexity of <span class="arithmatex">\(O(n^2)\)</span>, adaptive sorting.</strong> Each round of "bubbling" traverses array segments of length <span class="arithmatex">\(n - 1\)</span>, <span class="arithmatex">\(n - 2\)</span>, <span class="arithmatex">\(\dots\)</span>, <span class="arithmatex">\(2\)</span>, <span class="arithmatex">\(1\)</span>, which sums to <span class="arithmatex">\((n - 1) n / 2\)</span>. With a <code>flag</code> optimization, the best-case time complexity can reach <span class="arithmatex">\(O(n)\)</span> when the array is already sorted.</li>
<li><strong>Space complexity of <span class="arithmatex">\(O(1)\)</span>, in-place sorting.</strong> Only a constant amount of extra space is used by pointers <span class="arithmatex">\(i\)</span> and <span class="arithmatex">\(j\)</span>.</li>
<li><strong>Stable sorting.</strong> Because equal elements are not swapped during "bubbling," their original order is preserved, making this a stable sort.</li>
</ul>
<!-- Source file information -->
+11 -11
View File
@@ -3602,10 +3602,10 @@
<!-- Page content -->
<h1 id="118-bucket-sort">11.8 &nbsp; Bucket sort<a class="headerlink" href="#118-bucket-sort" title="Permanent link">&para;</a></h1>
<p>The previously mentioned sorting algorithms are all "comparison-based sorting algorithms," which sort by comparing the size of elements. Such sorting algorithms cannot surpass a time complexity of <span class="arithmatex">\(O(n \log n)\)</span>. Next, we will discuss several "non-comparison sorting algorithms" that can achieve linear time complexity.</p>
<p><u>Bucket sort</u> is a typical application of the divide-and-conquer strategy. It involves setting up a series of ordered buckets, each corresponding to a range of data, and then distributing the data evenly among these buckets; each bucket is then sorted individually; finally, all the data are merged in the order of the buckets.</p>
<p>The previously mentioned sorting algorithms are all "comparison-based sorting algorithms," which sort elements by comparing their values. Such sorting algorithms cannot have better time complexity of <span class="arithmatex">\(O(n \log n)\)</span>. Next, we will discuss several "non-comparison sorting algorithms" that could achieve linear time complexity.</p>
<p><u>Bucket sort</u> is a typical application of the divide-and-conquer strategy. It works by setting up a series of ordered buckets, each containing a range of data, and distributing the input data evenly across these buckets. And then, the data in each bucket is sorted individually. Finally, the sorted data from all the buckets is merged in sequence to produce the final result.</p>
<h2 id="1181-algorithm-process">11.8.1 &nbsp; Algorithm process<a class="headerlink" href="#1181-algorithm-process" title="Permanent link">&para;</a></h2>
<p>Consider an array of length <span class="arithmatex">\(n\)</span>, with elements in the range <span class="arithmatex">\([0, 1)\)</span>. The bucket sort process is illustrated in Figure 11-13.</p>
<p>Consider an array of length <span class="arithmatex">\(n\)</span>, with float numbers in the range <span class="arithmatex">\([0, 1)\)</span>. The bucket sort process is illustrated in Figure 11-13.</p>
<ol>
<li>Initialize <span class="arithmatex">\(k\)</span> buckets and distribute <span class="arithmatex">\(n\)</span> elements into these <span class="arithmatex">\(k\)</span> buckets.</li>
<li>Sort each bucket individually (using the built-in sorting function of the programming language).</li>
@@ -3747,21 +3747,21 @@
</div>
</div>
<h2 id="1182-algorithm-characteristics">11.8.2 &nbsp; Algorithm characteristics<a class="headerlink" href="#1182-algorithm-characteristics" title="Permanent link">&para;</a></h2>
<p>Bucket sort is suitable for handling very large data sets. For example, if the input data includes 1 million elements, and system memory limitations prevent loading all the data at once, you can divide the data into 1,000 buckets and sort each bucket separately before merging the results.</p>
<p>Bucket sort is suitable for handling very large data sets. For example, if the input data includes 1 million elements, and system memory limitations prevent loading all the data at the same time, you can divide the data into 1,000 buckets and sort each bucket separately before merging the results.</p>
<ul>
<li><strong>Time complexity is <span class="arithmatex">\(O(n + k)\)</span></strong>: Assuming the elements are evenly distributed across the buckets, the number of elements in each bucket is <span class="arithmatex">\(n/k\)</span>. Assuming sorting a single bucket takes <span class="arithmatex">\(O(n/k \log(n/k))\)</span> time, sorting all buckets takes <span class="arithmatex">\(O(n \log(n/k))\)</span> time. <strong>When the number of buckets <span class="arithmatex">\(k\)</span> is relatively large, the time complexity tends towards <span class="arithmatex">\(O(n)\)</span></strong>. Merging the results requires traversing all buckets and elements, taking <span class="arithmatex">\(O(n + k)\)</span> time. In the worst case, all data is distributed into a single bucket, and sorting that bucket takes <span class="arithmatex">\(O(n^2)\)</span> time.</li>
<li><strong>Time complexity is <span class="arithmatex">\(O(n + k)\)</span></strong>: Assuming the elements are evenly distributed across the buckets, the number of elements in each bucket is <span class="arithmatex">\(n/k\)</span>. Assuming sorting a single bucket takes <span class="arithmatex">\(O(n/k \log(n/k))\)</span> time, sorting all buckets takes <span class="arithmatex">\(O(n \log(n/k))\)</span> time. <strong>When the number of buckets <span class="arithmatex">\(k\)</span> is relatively large, the time complexity approaches <span class="arithmatex">\(O(n)\)</span></strong>. Merging the results requires traversing all buckets and elements, taking <span class="arithmatex">\(O(n + k)\)</span> time. In the worst case, all data is distributed into a single bucket, and sorting that bucket takes <span class="arithmatex">\(O(n^2)\)</span> time.</li>
<li><strong>Space complexity is <span class="arithmatex">\(O(n + k)\)</span>, non-in-place sorting</strong>: It requires additional space for <span class="arithmatex">\(k\)</span> buckets and a total of <span class="arithmatex">\(n\)</span> elements.</li>
<li>Whether bucket sort is stable depends on whether the algorithm used to sort elements within the buckets is stable.</li>
<li>Whether bucket sort is stable depends on whether the sorting algorithm used within each bucket is stable.</li>
</ul>
<h2 id="1183-how-to-achieve-even-distribution">11.8.3 &nbsp; How to achieve even distribution<a class="headerlink" href="#1183-how-to-achieve-even-distribution" title="Permanent link">&para;</a></h2>
<p>The theoretical time complexity of bucket sort can reach <span class="arithmatex">\(O(n)\)</span>, <strong>the key is to evenly distribute the elements across all buckets</strong>, as real data is often not uniformly distributed. For example, if we want to evenly distribute all products on Taobao by price range into 10 buckets, but the distribution of product prices is uneven, with many under 100 yuan and few over 1000 yuan. If the price range is evenly divided into 10, the difference in the number of products in each bucket will be very large.</p>
<p>To achieve even distribution, we can initially set a rough dividing line, roughly dividing the data into 3 buckets. <strong>After the distribution is complete, the buckets with more products can be further divided into 3 buckets, until the number of elements in all buckets is roughly equal</strong>.</p>
<p>As shown in Figure 11-14, this method essentially creates a recursive tree, aiming to make the leaf node values as even as possible. Of course, you don't have to divide the data into 3 buckets each round; the specific division method can be flexibly chosen based on data characteristics.</p>
<p>The theoretical time complexity of bucket sort can reach <span class="arithmatex">\(O(n)\)</span>. <strong>The key is to evenly distribute the elements across all buckets</strong> as real-world data is often not uniformly distributed. For example, we may want to evenly distribute all products on eBay by price range into 10 buckets. However, the distribution of product prices may not be even, with many under $100 and few over $500. If the price range is evenly divided into 10, the difference in the number of products in each bucket will be significant.</p>
<p>To achieve even distribution, we can initially set an approximate boundary to roughly divide the data into 3 buckets. <strong>After the distribution is complete, the buckets with more items can be further divided into 3 buckets, until the number of elements in all buckets is roughly equal</strong>.</p>
<p>As shown in Figure 11-14, this method essentially constructs a recursive tree, aiming to ensure the element counts in leaf nodes are as even as possible. Of course, you don't have to divide the data into 3 buckets each round - the partitioning strategy can be adaptively tailored to the data's unique characteristics.</p>
<p><a class="glightbox" href="../bucket_sort.assets/scatter_in_buckets_recursively.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Recursive division of buckets" class="animation-figure" src="../bucket_sort.assets/scatter_in_buckets_recursively.png" /></a></p>
<p align="center"> Figure 11-14 &nbsp; Recursive division of buckets </p>
<p>If we know the probability distribution of product prices in advance, <strong>we can set the price dividing line for each bucket based on the data probability distribution</strong>. It is worth noting that it is not necessarily required to specifically calculate the data distribution; it can also be approximated based on data characteristics using some probability model.</p>
<p>As shown in Figure 11-15, we assume that product prices follow a normal distribution, allowing us to reasonably set the price intervals, thereby evenly distributing the products into the respective buckets.</p>
<p>If we know the probability distribution of product prices in advance, <strong>we can set the price boundaries for each bucket based on the data probability distribution</strong>. It is worth noting that it is not necessarily required to specifically calculate the data distribution; instead, it can be approximated based on data characteristics using a probability model.</p>
<p>As shown in Figure 11-15, assuming that product prices follow a normal distribution, we can define reasonable price intervals to balance the distribution of items across the buckets.</p>
<p><a class="glightbox" href="../bucket_sort.assets/scatter_in_buckets_distribution.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Dividing buckets based on probability distribution" class="animation-figure" src="../bucket_sort.assets/scatter_in_buckets_distribution.png" /></a></p>
<p align="center"> Figure 11-15 &nbsp; Dividing buckets based on probability distribution </p>
+10 -10
View File
@@ -3620,13 +3620,13 @@
<!-- Page content -->
<h1 id="119-counting-sort">11.9 &nbsp; Counting sort<a class="headerlink" href="#119-counting-sort" title="Permanent link">&para;</a></h1>
<p><u>Counting sort</u> achieves sorting by counting the number of elements, typically applied to arrays of integers.</p>
<p><u>Counting sort</u> achieves sorting by counting the number of elements, usually applied to integer arrays.</p>
<h2 id="1191-simple-implementation">11.9.1 &nbsp; Simple implementation<a class="headerlink" href="#1191-simple-implementation" title="Permanent link">&para;</a></h2>
<p>Let's start with a simple example. Given an array <code>nums</code> of length <span class="arithmatex">\(n\)</span>, where all elements are "non-negative integers", the overall process of counting sort is illustrated in Figure 11-16.</p>
<p>Let's start with a simple example. Given an array <code>nums</code> of length <span class="arithmatex">\(n\)</span>, where all elements are "non-negative integers", the overall process of counting sort is shown in Figure 11-16.</p>
<ol>
<li>Traverse the array to find the maximum number, denoted as <span class="arithmatex">\(m\)</span>, then create an auxiliary array <code>counter</code> of length <span class="arithmatex">\(m + 1\)</span>.</li>
<li><strong>Use <code>counter</code> to count the occurrence of each number in <code>nums</code></strong>, where <code>counter[num]</code> corresponds to the occurrence of the number <code>num</code>. The counting method is simple, just traverse <code>nums</code> (suppose the current number is <code>num</code>), and increase <code>counter[num]</code> by <span class="arithmatex">\(1\)</span> each round.</li>
<li><strong>Since the indices of <code>counter</code> are naturally ordered, all numbers are essentially sorted already</strong>. Next, we traverse <code>counter</code>, filling <code>nums</code> in ascending order of occurrence.</li>
<li><strong>Since the indices of <code>counter</code> are naturally ordered, all numbers are essentially sorted already</strong>. Next, we traverse <code>counter</code>, and fill in <code>nums</code> in ascending order of occurrence.</li>
</ol>
<p><a class="glightbox" href="../counting_sort.assets/counting_sort_overview.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Counting sort process" class="animation-figure" src="../counting_sort.assets/counting_sort_overview.png" /></a></p>
<p align="center"> Figure 11-16 &nbsp; Counting sort process </p>
@@ -3756,15 +3756,15 @@
<p>From the perspective of bucket sort, we can consider each index of the counting array <code>counter</code> in counting sort as a bucket, and the process of counting as distributing elements into the corresponding buckets. Essentially, counting sort is a special case of bucket sort for integer data.</p>
</div>
<h2 id="1192-complete-implementation">11.9.2 &nbsp; Complete implementation<a class="headerlink" href="#1192-complete-implementation" title="Permanent link">&para;</a></h2>
<p>Astute readers might have noticed, <strong>if the input data is an object, the above step <code>3.</code> becomes ineffective</strong>. Suppose the input data is a product object, we want to sort the products by their price (a class member variable), but the above algorithm can only provide the sorting result for the price.</p>
<p>Observant readers might notice, <strong>if the input data is an object, the above step <code>3.</code> is invalid</strong>. Suppose the input data is a product object, we want to sort the products by the price (a class member variable), but the above algorithm can only give the sorted price as the result.</p>
<p>So how can we get the sorting result for the original data? First, we calculate the "prefix sum" of <code>counter</code>. As the name suggests, the prefix sum at index <code>i</code>, <code>prefix[i]</code>, equals the sum of the first <code>i</code> elements of the array:</p>
<div class="arithmatex">\[
\text{prefix}[i] = \sum_{j=0}^i \text{counter[j]}
\]</div>
<p><strong>The prefix sum has a clear meaning, <code>prefix[num] - 1</code> represents the last occurrence index of element <code>num</code> in the result array <code>res</code></strong>. This information is crucial, as it tells us where each element should appear in the result array. Next, we traverse the original array <code>nums</code> for each element <code>num</code> in reverse order, performing the following two steps in each iteration.</p>
<p><strong>The prefix sum has a clear meaning, <code>prefix[num] - 1</code> represents the index of the last occurrence of element <code>num</code> in the result array <code>res</code></strong>. This information is crucial, as it tells us where each element should appear in the result array. Next, we traverse each element <code>num</code> of the original array <code>nums</code> in reverse order, performing the following two steps in each iteration.</p>
<ol>
<li>Fill <code>num</code> into the array <code>res</code> at the index <code>prefix[num] - 1</code>.</li>
<li>Reduce the prefix sum <code>prefix[num]</code> by <span class="arithmatex">\(1\)</span>, thus obtaining the next index to place <code>num</code>.</li>
<li>Decrease the prefix sum <code>prefix[num]</code> by <span class="arithmatex">\(1\)</span> to obtain the next index to place <code>num</code>.</li>
</ol>
<p>After the traversal, the array <code>res</code> contains the sorted result, and finally, <code>res</code> replaces the original array <code>nums</code>. The complete counting sort process is shown in Figure 11-17.</p>
<div class="tabbed-set tabbed-alternate" data-tabs="2:8"><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" /><div class="tabbed-labels"><label for="__tabbed_2_1">&lt;1&gt;</label><label for="__tabbed_2_2">&lt;2&gt;</label><label for="__tabbed_2_3">&lt;3&gt;</label><label for="__tabbed_2_4">&lt;4&gt;</label><label for="__tabbed_2_5">&lt;5&gt;</label><label for="__tabbed_2_6">&lt;6&gt;</label><label for="__tabbed_2_7">&lt;7&gt;</label><label for="__tabbed_2_8">&lt;8&gt;</label></div>
@@ -3946,14 +3946,14 @@
</div>
<h2 id="1193-algorithm-characteristics">11.9.3 &nbsp; Algorithm characteristics<a class="headerlink" href="#1193-algorithm-characteristics" title="Permanent link">&para;</a></h2>
<ul>
<li><strong>Time complexity is <span class="arithmatex">\(O(n + m)\)</span>, non-adaptive sort</strong>: Involves traversing <code>nums</code> and <code>counter</code>, both using linear time. Generally, <span class="arithmatex">\(n \gg m\)</span>, and the time complexity tends towards <span class="arithmatex">\(O(n)\)</span>.</li>
<li><strong>Space complexity is <span class="arithmatex">\(O(n + m)\)</span>, non-in-place sort</strong>: Utilizes arrays <code>res</code> and <code>counter</code> of lengths <span class="arithmatex">\(n\)</span> and <span class="arithmatex">\(m\)</span> respectively.</li>
<li><strong>Time complexity is <span class="arithmatex">\(O(n + m)\)</span>, non-adaptive sort</strong>: It involves traversing <code>nums</code> and <code>counter</code>, both using linear time. Generally, <span class="arithmatex">\(n \gg m\)</span>, and the time complexity tends towards <span class="arithmatex">\(O(n)\)</span>.</li>
<li><strong>Space complexity is <span class="arithmatex">\(O(n + m)\)</span>, non-in-place sort</strong>: It uses array <code>res</code> of lengths <span class="arithmatex">\(n\)</span> and array <code>counter</code> of length <span class="arithmatex">\(m\)</span> respectively.</li>
<li><strong>Stable sort</strong>: Since elements are filled into <code>res</code> in a "right-to-left" order, reversing the traversal of <code>nums</code> can prevent changing the relative position between equal elements, thereby achieving a stable sort. Actually, traversing <code>nums</code> in order can also produce the correct sorting result, but the outcome is unstable.</li>
</ul>
<h2 id="1194-limitations">11.9.4 &nbsp; Limitations<a class="headerlink" href="#1194-limitations" title="Permanent link">&para;</a></h2>
<p>By now, you might find counting sort very clever, as it can achieve efficient sorting merely by counting quantities. However, the prerequisites for using counting sort are relatively strict.</p>
<p><strong>Counting sort is only suitable for non-negative integers</strong>. If you want to apply it to other types of data, you need to ensure that these data can be converted to non-negative integers without changing the relative sizes of the elements. For example, for an array containing negative integers, you can first add a constant to all numbers, converting them all to positive numbers, and then convert them back after sorting is complete.</p>
<p><strong>Counting sort is suitable for large data volumes but small data ranges</strong>. For example, in the above example, <span class="arithmatex">\(m\)</span> should not be too large, otherwise, it will occupy too much space. And when <span class="arithmatex">\(n \ll m\)</span>, counting sort uses <span class="arithmatex">\(O(m)\)</span> time, which may be slower than <span class="arithmatex">\(O(n \log n)\)</span> sorting algorithms.</p>
<p><strong>Counting sort is only suitable for non-negative integers</strong>. If you want to apply it to other types of data, you need to ensure that these data can be converted to non-negative integers without changing the original order of the elements. For example, for an array containing negative integers, you can first add a constant to all numbers, converting them all to positive numbers, and then convert them back after sorting is complete.</p>
<p><strong>Counting sort is suitable for large datasets with a small range of values</strong>. For example, in the above example, <span class="arithmatex">\(m\)</span> should not be too large, otherwise, it will occupy too much space. And when <span class="arithmatex">\(n \ll m\)</span>, counting sort uses <span class="arithmatex">\(O(m)\)</span> time, which may be slower than <span class="arithmatex">\(O(n \log n)\)</span> sorting algorithms.</p>
<!-- Source file information -->
+7 -7
View File
@@ -3586,25 +3586,25 @@
<h1 id="117-heap-sort">11.7 &nbsp; Heap sort<a class="headerlink" href="#117-heap-sort" title="Permanent link">&para;</a></h1>
<div class="admonition tip">
<p class="admonition-title">Tip</p>
<p>Before reading this section, please make sure you have completed the "Heap" chapter.</p>
<p>Before reading this section, please ensure you have completed the "Heap" chapter.</p>
</div>
<p><u>Heap sort</u> is an efficient sorting algorithm based on the heap data structure. We can implement heap sort using the "heap creation" and "element extraction" operations we have already learned.</p>
<ol>
<li>Input the array and establish a min-heap, where the smallest element is at the heap's top.</li>
<li>Continuously perform the extraction operation, recording the extracted elements in sequence to obtain a sorted list from smallest to largest.</li>
<li>Input the array and construct a min-heap, where the smallest element is at the top of the heap.</li>
<li>Continuously perform the extraction operation, record the extracted elements sequentially to obtain a sorted list from smallest to largest.</li>
</ol>
<p>Although the above method is feasible, it requires an additional array to save the popped elements, which is somewhat space-consuming. In practice, we usually use a more elegant implementation.</p>
<p>Although the above method is feasible, it requires an additional array to store the popped elements, which is somewhat space-consuming. In practice, we usually use a more elegant implementation.</p>
<h2 id="1171-algorithm-flow">11.7.1 &nbsp; Algorithm flow<a class="headerlink" href="#1171-algorithm-flow" title="Permanent link">&para;</a></h2>
<p>Suppose the array length is <span class="arithmatex">\(n\)</span>, the heap sort process is as follows.</p>
<ol>
<li>Input the array and establish a max-heap. After completion, the largest element is at the heap's top.</li>
<li>Swap the top element of the heap (the first element) with the heap's bottom element (the last element). After the swap, reduce the heap's length by <span class="arithmatex">\(1\)</span> and increase the sorted elements count by <span class="arithmatex">\(1\)</span>.</li>
<li>Input the array and establish a max-heap. After this step, the largest element is positioned at the top of the heap.</li>
<li>Swap the top element of the heap (the first element) with the heap's bottom element (the last element). Following this swap, reduce the heap's length by <span class="arithmatex">\(1\)</span> and increase the sorted elements count by <span class="arithmatex">\(1\)</span>.</li>
<li>Starting from the heap top, perform the sift-down operation from top to bottom. After the sift-down, the heap's property is restored.</li>
<li>Repeat steps <code>2.</code> and <code>3.</code> Loop for <span class="arithmatex">\(n - 1\)</span> rounds to complete the sorting of the array.</li>
</ol>
<div class="admonition tip">
<p class="admonition-title">Tip</p>
<p>In fact, the element extraction operation also includes steps <code>2.</code> and <code>3.</code>, with the addition of a popping element step.</p>
<p>In fact, the element extraction operation also includes steps <code>2.</code> and <code>3.</code>, with an additional step to pop (remove) the extracted element from the heap.</p>
</div>
<div class="tabbed-set tabbed-alternate" data-tabs="1:12"><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" /><div class="tabbed-labels"><label for="__tabbed_1_1">&lt;1&gt;</label><label for="__tabbed_1_2">&lt;2&gt;</label><label for="__tabbed_1_3">&lt;3&gt;</label><label for="__tabbed_1_4">&lt;4&gt;</label><label for="__tabbed_1_5">&lt;5&gt;</label><label for="__tabbed_1_6">&lt;6&gt;</label><label for="__tabbed_1_7">&lt;7&gt;</label><label for="__tabbed_1_8">&lt;8&gt;</label><label for="__tabbed_1_9">&lt;9&gt;</label><label for="__tabbed_1_10">&lt;10&gt;</label><label for="__tabbed_1_11">&lt;11&gt;</label><label for="__tabbed_1_12">&lt;12&gt;</label></div>
<div class="tabbed-content">
+1 -1
View File
@@ -3520,7 +3520,7 @@
<p><a class="glightbox" href="../assets/covers/chapter_sorting.jpg" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Sorting" class="cover-image" src="../assets/covers/chapter_sorting.jpg" /></a></p>
<div class="admonition abstract">
<p class="admonition-title">Abstract</p>
<p>Sorting is like a magical key that turns chaos into order, enabling us to understand and handle data in a more efficient manner.</p>
<p>Sorting is like a magical key that turns chaos into order, enabling us to understand and handle data more efficiently.</p>
<p>Whether it's simple ascending order or complex categorical arrangements, sorting reveals the harmonious beauty of data.</p>
</div>
<h2 id="chapter-contents">Chapter contents<a class="headerlink" href="#chapter-contents" title="Permanent link">&para;</a></h2>
+10 -10
View File
@@ -3603,18 +3603,18 @@
<!-- Page content -->
<h1 id="114-insertion-sort">11.4 &nbsp; Insertion sort<a class="headerlink" href="#114-insertion-sort" title="Permanent link">&para;</a></h1>
<p><u>Insertion sort</u> is a simple sorting algorithm that works very much like the process of manually sorting a deck of cards.</p>
<p>Specifically, we select a pivot element from the unsorted interval, compare it with the elements in the sorted interval to its left, and insert the element into the correct position.</p>
<p>Figure 11-6 shows the process of inserting an element into an array. Assuming the pivot element is <code>base</code>, we need to move all elements between the target index and <code>base</code> one position to the right, then assign <code>base</code> to the target index.</p>
<p>Specifically, we select a base element from the unsorted interval, compare it with the elements in the sorted interval to its left, and insert the element into the correct position.</p>
<p>Figure 11-6 illustrates how an element is inserted into the array. Assuming the base element is <code>base</code>, we need to shift all elements from the target index up to <code>base</code> one position to the right, then assign <code>base</code> to the target index.</p>
<p><a class="glightbox" href="../insertion_sort.assets/insertion_operation.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Single insertion operation" class="animation-figure" src="../insertion_sort.assets/insertion_operation.png" /></a></p>
<p align="center"> Figure 11-6 &nbsp; Single insertion operation </p>
<h2 id="1141-algorithm-process">11.4.1 &nbsp; Algorithm process<a class="headerlink" href="#1141-algorithm-process" title="Permanent link">&para;</a></h2>
<p>The overall process of insertion sort is shown in Figure 11-7.</p>
<ol>
<li>Initially, the first element of the array is sorted.</li>
<li>The second element of the array is taken as <code>base</code>, and after inserting it into the correct position, <strong>the first two elements of the array are sorted</strong>.</li>
<li>The third element is taken as <code>base</code>, and after inserting it into the correct position, <strong>the first three elements of the array are sorted</strong>.</li>
<li>And so on, in the last round, the last element is taken as <code>base</code>, and after inserting it into the correct position, <strong>all elements are sorted</strong>.</li>
<li>Consider the first element of the array as sorted.</li>
<li>Select the second element as <code>base</code>, insert it into its correct position, <strong>leaving the first two elements sorted</strong>.</li>
<li>Select the third element as <code>base</code>, insert it into its correct position, <strong>leaving the first three elements sorted</strong>.</li>
<li>Continuing in this manner, in the final iteration, the last element is taken as <code>base</code>, and after inserting it into the correct position, <strong>all elements are sorted</strong>.</li>
</ol>
<p><a class="glightbox" href="../insertion_sort.assets/insertion_sort_overview.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Insertion sort process" class="animation-figure" src="../insertion_sort.assets/insertion_sort_overview.png" /></a></p>
<p align="center"> Figure 11-7 &nbsp; Insertion sort process </p>
@@ -3721,10 +3721,10 @@
<li><strong>Stable sorting</strong>: During the insertion operation, we insert elements to the right of equal elements, not changing their order.</li>
</ul>
<h2 id="1143-advantages-of-insertion-sort">11.4.3 &nbsp; Advantages of insertion sort<a class="headerlink" href="#1143-advantages-of-insertion-sort" title="Permanent link">&para;</a></h2>
<p>The time complexity of insertion sort is <span class="arithmatex">\(O(n^2)\)</span>, while the time complexity of quicksort, which we will study next, is <span class="arithmatex">\(O(n \log n)\)</span>. Although insertion sort has a higher time complexity, <strong>it is usually faster in cases of small data volumes</strong>.</p>
<p>This conclusion is similar to that for linear and binary search. Algorithms like quicksort that have a time complexity of <span class="arithmatex">\(O(n \log n)\)</span> and are based on the divide-and-conquer strategy often involve more unit operations. In cases of small data volumes, the numerical values of <span class="arithmatex">\(n^2\)</span> and <span class="arithmatex">\(n \log n\)</span> are close, and complexity does not dominate, with the number of unit operations per round playing a decisive role.</p>
<p>In fact, many programming languages (such as Java) use insertion sort in their built-in sorting functions. The general approach is: for long arrays, use sorting algorithms based on divide-and-conquer strategies, such as quicksort; for short arrays, use insertion sort directly.</p>
<p>Although bubble sort, selection sort, and insertion sort all have a time complexity of <span class="arithmatex">\(O(n^2)\)</span>, in practice, <strong>insertion sort is used significantly more frequently than bubble sort and selection sort</strong>, mainly for the following reasons.</p>
<p>The time complexity of insertion sort is <span class="arithmatex">\(O(n^2)\)</span>, while the time complexity of quicksort, which we will study next, is <span class="arithmatex">\(O(n \log n)\)</span>. Although insertion sort has a higher time complexity, <strong>it is usually faster in small input sizes</strong>.</p>
<p>This conclusion is similar to that for linear and binary search. Algorithms like quicksort that have a time complexity of <span class="arithmatex">\(O(n \log n)\)</span> and are based on the divide-and-conquer strategy often involve more unit operations. For small input sizes, the numerical values of <span class="arithmatex">\(n^2\)</span> and <span class="arithmatex">\(n \log n\)</span> are close, and complexity does not dominate, with the number of unit operations per round playing a decisive role.</p>
<p>In fact, many programming languages (such as Java) use insertion sort within their built-in sorting functions. The general approach is: for long arrays, use sorting algorithms based on divide-and-conquer strategies, such as quicksort; for short arrays, use insertion sort directly.</p>
<p>Although bubble sort, selection sort, and insertion sort all have a time complexity of <span class="arithmatex">\(O(n^2)\)</span>, in practice, <strong>insertion sort is commonly used than bubble sort and selection sort</strong>, mainly for the following reasons.</p>
<ul>
<li>Bubble sort is based on element swapping, which requires the use of a temporary variable, involving 3 unit operations; insertion sort is based on element assignment, requiring only 1 unit operation. Therefore, <strong>the computational overhead of bubble sort is generally higher than that of insertion sort</strong>.</li>
<li>The time complexity of selection sort is always <span class="arithmatex">\(O(n^2)\)</span>. <strong>Given a set of partially ordered data, insertion sort is usually more efficient than selection sort</strong>.</li>
+10 -10
View File
@@ -3604,8 +3604,8 @@
<h1 id="116-merge-sort">11.6 &nbsp; Merge sort<a class="headerlink" href="#116-merge-sort" title="Permanent link">&para;</a></h1>
<p><u>Merge sort</u> is a sorting algorithm based on the divide-and-conquer strategy, involving the "divide" and "merge" phases shown in Figure 11-10.</p>
<ol>
<li><strong>Divide phase</strong>: Recursively split the array from the midpoint, transforming the sorting problem of a long array into that of shorter arrays.</li>
<li><strong>Merge phase</strong>: Stop dividing when the length of the sub-array is 1, start merging, and continuously combine two shorter ordered arrays into one longer ordered array until the process is complete.</li>
<li><strong>Divide phase</strong>: Recursively split the array from the midpoint, transforming the sorting problem of a long array into shorter arrays.</li>
<li><strong>Merge phase</strong>: Stop dividing when the length of the sub-array is 1, and then begin merging. The two shorter sorted arrays are continuously merged into a longer sorted array until the process is complete.</li>
</ol>
<p><a class="glightbox" href="../merge_sort.assets/merge_sort_overview.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="The divide and merge phases of merge sort" class="animation-figure" src="../merge_sort.assets/merge_sort_overview.png" /></a></p>
<p align="center"> Figure 11-10 &nbsp; The divide and merge phases of merge sort </p>
@@ -3614,9 +3614,9 @@
<p>As shown in Figure 11-11, the "divide phase" recursively splits the array from the midpoint into two sub-arrays from top to bottom.</p>
<ol>
<li>Calculate the midpoint <code>mid</code>, recursively divide the left sub-array (interval <code>[left, mid]</code>) and the right sub-array (interval <code>[mid + 1, right]</code>).</li>
<li>Continue with step <code>1.</code> recursively until the sub-array interval length is 1 to stop.</li>
<li>Continue with step <code>1.</code> recursively until sub-array length becomes 1, then stops.</li>
</ol>
<p>The "merge phase" combines the left and right sub-arrays into a single ordered array from bottom to top. Note that merging starts with sub-arrays of length 1, and each sub-array is ordered during the merge phase.</p>
<p>The "merge phase" combines the left and right sub-arrays into a sorted array from bottom to top. It is important to note that, merging starts with sub-arrays of length 1, and each sub-array is sorted during the merge phase.</p>
<div class="tabbed-set tabbed-alternate" data-tabs="1:10"><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" /><div class="tabbed-labels"><label for="__tabbed_1_1">&lt;1&gt;</label><label for="__tabbed_1_2">&lt;2&gt;</label><label for="__tabbed_1_3">&lt;3&gt;</label><label for="__tabbed_1_4">&lt;4&gt;</label><label for="__tabbed_1_5">&lt;5&gt;</label><label for="__tabbed_1_6">&lt;6&gt;</label><label for="__tabbed_1_7">&lt;7&gt;</label><label for="__tabbed_1_8">&lt;8&gt;</label><label for="__tabbed_1_9">&lt;9&gt;</label><label for="__tabbed_1_10">&lt;10&gt;</label></div>
<div class="tabbed-content">
<div class="tabbed-block">
@@ -3653,10 +3653,10 @@
</div>
<p align="center"> Figure 11-11 &nbsp; Merge sort process </p>
<p>It is observed that the order of recursion in merge sort is consistent with the post-order traversal of a binary tree.</p>
<p>It can be observed that the order of recursion in merge sort is consistent with the post-order traversal of a binary tree.</p>
<ul>
<li><strong>Post-order traversal</strong>: First recursively traverse the left subtree, then the right subtree, and finally handle the root node.</li>
<li><strong>Merge sort</strong>: First recursively handle the left sub-array, then the right sub-array, and finally perform the merge.</li>
<li><strong>Post-order traversal</strong>: First recursively traverse the left subtree, then the right subtree, and finally process the root node.</li>
<li><strong>Merge sort</strong>: First recursively process the left sub-array, then the right sub-array, and finally perform the merge.</li>
</ul>
<p>The implementation of merge sort is shown in the following code. Note that the interval to be merged in <code>nums</code> is <code>[left, right]</code>, while the corresponding interval in <code>tmp</code> is <code>[0, right - left]</code>.</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>
@@ -3863,12 +3863,12 @@
<li><strong>Stable sort</strong>: During the merging process, the order of equal elements remains unchanged.</li>
</ul>
<h2 id="1163-linked-list-sorting">11.6.3 &nbsp; Linked List sorting<a class="headerlink" href="#1163-linked-list-sorting" title="Permanent link">&para;</a></h2>
<p>For linked lists, merge sort has significant advantages over other sorting algorithms, <strong>optimizing the space complexity of the linked list sorting task to <span class="arithmatex">\(O(1)\)</span></strong>.</p>
<p>For linked lists, merge sort has significant advantages over other sorting algorithms. <strong>It can optimize the space complexity of the linked list sorting task to <span class="arithmatex">\(O(1)\)</span></strong>.</p>
<ul>
<li><strong>Divide phase</strong>: "Iteration" can be used instead of "recursion" to perform the linked list division work, thus saving the stack frame space used by recursion.</li>
<li><strong>Merge phase</strong>: In linked lists, node addition and deletion operations can be achieved by changing references (pointers), so no extra lists need to be created during the merge phase (combining two short ordered lists into one long ordered list).</li>
<li><strong>Merge phase</strong>: In linked lists, node insertion and deletion operations can be achieved by changing references (pointers), so no extra lists need to be created during the merge phase (combining two short ordered lists into one long ordered list).</li>
</ul>
<p>Detailed implementation details are complex, and interested readers can consult related materials for learning.</p>
<p>The implementation details are relatively complex, and interested readers can consult related materials for learning.</p>
<!-- Source file information -->
+19 -18
View File
@@ -3638,11 +3638,11 @@
<!-- Page content -->
<h1 id="115-quick-sort">11.5 &nbsp; Quick sort<a class="headerlink" href="#115-quick-sort" title="Permanent link">&para;</a></h1>
<p><u>Quick sort</u> is a sorting algorithm based on the divide and conquer strategy, known for its efficiency and wide application.</p>
<p>The core operation of quick sort is "pivot partitioning," aiming to: select an element from the array as the "pivot," move all elements smaller than the pivot to its left, and move elements greater than the pivot to its right. Specifically, the pivot partitioning process is illustrated in Figure 11-8.</p>
<p><u>Quick sort</u> is a sorting algorithm based on the divide-and-conquer strategy, known for its efficiency and wide application.</p>
<p>The core operation of quick sort is "pivot partitioning," which aims to select an element from the array as the "pivot" and move all elements less than the pivot to its left side, while moving all elements greater than the pivot to its right side. Specifically, the process of pivot partitioning is illustrated in Figure 11-8.</p>
<ol>
<li>Select the leftmost element of the array as the pivot, and initialize two pointers <code>i</code> and <code>j</code> at both ends of the array.</li>
<li>Set up a loop where each round uses <code>i</code> (<code>j</code>) to find the first element larger (smaller) than the pivot, then swap these two elements.</li>
<li>Select the leftmost element of the array as the pivot, and initialize two pointers <code>i</code> and <code>j</code> to point to the two ends of the array respectively.</li>
<li>Set up a loop where each round uses <code>i</code> (<code>j</code>) to search for the first element larger (smaller) than the pivot, then swap these two elements.</li>
<li>Repeat step <code>2.</code> until <code>i</code> and <code>j</code> meet, finally swap the pivot to the boundary between the two sub-arrays.</li>
</ol>
<div class="tabbed-set tabbed-alternate" data-tabs="1:9"><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" /><div class="tabbed-labels"><label for="__tabbed_1_1">&lt;1&gt;</label><label for="__tabbed_1_2">&lt;2&gt;</label><label for="__tabbed_1_3">&lt;3&gt;</label><label for="__tabbed_1_4">&lt;4&gt;</label><label for="__tabbed_1_5">&lt;5&gt;</label><label for="__tabbed_1_6">&lt;6&gt;</label><label for="__tabbed_1_7">&lt;7&gt;</label><label for="__tabbed_1_8">&lt;8&gt;</label><label for="__tabbed_1_9">&lt;9&gt;</label></div>
@@ -3678,10 +3678,10 @@
</div>
<p align="center"> Figure 11-8 &nbsp; Pivot division process </p>
<p>After the pivot partitioning, the original array is divided into three parts: left sub-array, pivot, and right sub-array, satisfying "any element in the left sub-array <span class="arithmatex">\(\leq\)</span> pivot <span class="arithmatex">\(\leq\)</span> any element in the right sub-array." Therefore, we only need to sort these two sub-arrays next.</p>
<p>After the pivot partitioning, the original array is divided into three parts: left sub-array, pivot, and right sub-array, satisfying "any element in the left sub-array <span class="arithmatex">\(\leq\)</span> pivot <span class="arithmatex">\(\leq\)</span> any element in the right sub-array." Therefore, we then only need to sort these two sub-arrays.</p>
<div class="admonition note">
<p class="admonition-title">Quick sort's divide and conquer strategy</p>
<p>The essence of pivot partitioning is to simplify a longer array's sorting problem into two shorter arrays' sorting problems.</p>
<p class="admonition-title">Divide-and-conquer strategy for quick sort</p>
<p>The essence of pivot partitioning is to simplify the sorting problem of a longer array into two shorter arrays.</p>
</div>
<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>
<div class="tabbed-content">
@@ -3807,8 +3807,8 @@
<p>The overall process of quick sort is shown in Figure 11-9.</p>
<ol>
<li>First, perform a "pivot partitioning" on the original array to obtain the unsorted left and right sub-arrays.</li>
<li>Then, recursively perform "pivot partitioning" on both the left and right sub-arrays.</li>
<li>Continue recursively until the sub-array length reaches 1, thus completing the sorting of the entire array.</li>
<li>Then, recursively perform "pivot partitioning" on the left and right sub-arrays separately.</li>
<li>Continue recursively until the length of sub-array is 1, thus completing the sorting of the entire array.</li>
</ol>
<p><a class="glightbox" href="../quick_sort.assets/quick_sort_overview.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Quick sort process" class="animation-figure" src="../quick_sort.assets/quick_sort_overview.png" /></a></p>
<p align="center"> Figure 11-9 &nbsp; Quick sort process </p>
@@ -3904,22 +3904,22 @@
</div>
<h2 id="1152-algorithm-features">11.5.2 &nbsp; Algorithm features<a class="headerlink" href="#1152-algorithm-features" title="Permanent link">&para;</a></h2>
<ul>
<li><strong>Time complexity of <span class="arithmatex">\(O(n \log n)\)</span>, non-adaptive sorting</strong>: In average cases, the recursive levels of pivot partitioning are <span class="arithmatex">\(\log n\)</span>, and the total number of loops per level is <span class="arithmatex">\(n\)</span>, using <span class="arithmatex">\(O(n \log n)\)</span> time overall. In the worst case, each round of pivot partitioning divides an array of length <span class="arithmatex">\(n\)</span> into two sub-arrays of lengths <span class="arithmatex">\(0\)</span> and <span class="arithmatex">\(n - 1\)</span>, reaching <span class="arithmatex">\(n\)</span> recursive levels, and using <span class="arithmatex">\(O(n^2)\)</span> time overall.</li>
<li><strong>Space complexity of <span class="arithmatex">\(O(n)\)</span>, in-place sorting</strong>: In completely reversed input arrays, reaching the worst recursion depth of <span class="arithmatex">\(n\)</span>, using <span class="arithmatex">\(O(n)\)</span> stack frame space. The sorting operation is performed on the original array without the aid of additional arrays.</li>
<li><strong>Time complexity of <span class="arithmatex">\(O(n \log n)\)</span>, non-adaptive sorting</strong>: In average cases, the recursive levels of pivot partitioning are <span class="arithmatex">\(\log n\)</span>, and the total number of loops per level is <span class="arithmatex">\(n\)</span>, using <span class="arithmatex">\(O(n \log n)\)</span> time overall. In the worst case, each round of pivot partitioning divides an array of length <span class="arithmatex">\(n\)</span> into two sub-arrays of lengths <span class="arithmatex">\(0\)</span> and <span class="arithmatex">\(n - 1\)</span>, when the number of recursive levels reaches <span class="arithmatex">\(n\)</span>, the number of loops in each level is <span class="arithmatex">\(n\)</span>, and the total time used is <span class="arithmatex">\(O(n^2)\)</span>.</li>
<li><strong>Space complexity of <span class="arithmatex">\(O(n)\)</span>, in-place sorting</strong>: In the case where the input array is completely reversed, the worst recursive depth reaches <span class="arithmatex">\(n\)</span>, using <span class="arithmatex">\(O(n)\)</span> stack frame space. The sorting operation is performed on the original array without the aid of additional arrays.</li>
<li><strong>Non-stable sorting</strong>: In the final step of pivot partitioning, the pivot may be swapped to the right of equal elements.</li>
</ul>
<h2 id="1153-why-is-quick-sort-fast">11.5.3 &nbsp; Why is quick sort fast<a class="headerlink" href="#1153-why-is-quick-sort-fast" title="Permanent link">&para;</a></h2>
<p>From its name, it is apparent that quick sort should have certain efficiency advantages. Although the average time complexity of quick sort is the same as "merge sort" and "heap sort," quick sort is generally more efficient, mainly for the following reasons.</p>
<p>As the name suggests, quick sort should have certain advantages in terms of efficiency. Although the average time complexity of quick sort is the same as that of "merge sort" and "heap sort," it is generally more efficient for the following reasons.</p>
<ul>
<li><strong>Low probability of worst-case scenarios</strong>: Although the worst time complexity of quick sort is <span class="arithmatex">\(O(n^2)\)</span>, less stable than merge sort, in most cases, quick sort can operate under a time complexity of <span class="arithmatex">\(O(n \log n)\)</span>.</li>
<li><strong>High cache usage efficiency</strong>: During the pivot partitioning operation, the system can load the entire sub-array into the cache, thus accessing elements more efficiently. In contrast, algorithms like "heap sort" need to access elements in a jumping manner, lacking this feature.</li>
<li><strong>Small constant coefficient of complexity</strong>: Among the mentioned algorithms, quick sort has the fewest total number of comparisons, assignments, and swaps. This is similar to why "insertion sort" is faster than "bubble sort."</li>
<li><strong>High cache utilization</strong>: During the pivot partitioning operation, the system can load the entire sub-array into the cache, thus accessing elements more efficiently. In contrast, algorithms like "heap sort" need to access elements in a jumping manner, lacking this feature.</li>
<li><strong>Small constant coefficient of complexity</strong>: Among the three algorithms mentioned above, quick sort has the least total number of operations such as comparisons, assignments, and swaps. This is similar to why "insertion sort" is faster than "bubble sort."</li>
</ul>
<h2 id="1154-pivot-optimization">11.5.4 &nbsp; Pivot optimization<a class="headerlink" href="#1154-pivot-optimization" title="Permanent link">&para;</a></h2>
<p><strong>Quick sort's time efficiency may decrease under certain inputs</strong>. For example, if the input array is completely reversed, since we select the leftmost element as the pivot, after the pivot partitioning, the pivot is swapped to the array's right end, causing the left sub-array length to be <span class="arithmatex">\(n - 1\)</span> and the right sub-array length to be <span class="arithmatex">\(0\)</span>. If this recursion continues, each round of pivot partitioning will have a sub-array length of <span class="arithmatex">\(0\)</span>, and the divide and conquer strategy fails, degrading quick sort to a form similar to "bubble sort."</p>
<p>To avoid this situation, <strong>we can optimize the strategy for selecting the pivot in the pivot partitioning</strong>. For instance, we can randomly select an element as the pivot. However, if luck is not on our side, and we keep selecting suboptimal pivots, the efficiency is still not satisfactory.</p>
<p><strong>Quick sort's time efficiency may degrade under certain inputs</strong>. For example, if the input array is completely reversed, since we select the leftmost element as the pivot, after the pivot partitioning, the pivot is swapped to the array's right end, causing the left sub-array length to be <span class="arithmatex">\(n - 1\)</span> and the right sub-array length to be <span class="arithmatex">\(0\)</span>. Continuing this way, each round of pivot partitioning will have a sub-array length of <span class="arithmatex">\(0\)</span>, and the divide-and-conquer strategy fails, degrading quick sort to a form similar to "bubble sort."</p>
<p>To avoid this situation, <strong>we can optimize the pivot selection strategy in the pivot partitioning</strong>. For instance, we can randomly select an element as the pivot. However, if luck is not on our side, and we consistently select suboptimal pivots, the efficiency is still not satisfactory.</p>
<p>It's important to note that programming languages usually generate "pseudo-random numbers". If we construct a specific test case for a pseudo-random number sequence, the efficiency of quick sort may still degrade.</p>
<p>For further improvement, we can select three candidate elements (usually the first, last, and midpoint elements of the array), <strong>and use the median of these three candidate elements as the pivot</strong>. This significantly increases the probability that the pivot is "neither too small nor too large". Of course, we can also select more candidate elements to further enhance the algorithm's robustness. Using this method significantly reduces the probability of time complexity degradation to <span class="arithmatex">\(O(n^2)\)</span>.</p>
<p>For further improvement, we can select three candidate elements (usually the first, last, and midpoint elements of the array), <strong>and use the median of these three candidate elements as the pivot</strong>. This way, the probability that the pivot is "neither too small nor too large" will be greatly increased. Of course, we can also select more candidate elements to further enhance robustness of the algorithm. With this method, the probability of the time complexity degrading to <span class="arithmatex">\(O(n^2)\)</span> is greatly reduced.</p>
<p>Sample code is as follows:</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">
@@ -4084,7 +4084,8 @@
</div>
</div>
<h2 id="1155-tail-recursion-optimization">11.5.5 &nbsp; Tail recursion optimization<a class="headerlink" href="#1155-tail-recursion-optimization" title="Permanent link">&para;</a></h2>
<p><strong>Under certain inputs, quick sort may occupy more space</strong>. For a completely ordered input array, assume the sub-array length in recursion is <span class="arithmatex">\(m\)</span>, each round of pivot partitioning produces a left sub-array of length <span class="arithmatex">\(0\)</span> and a right sub-array of length <span class="arithmatex">\(m - 1\)</span>, meaning the problem size reduced per recursive call is very small (only one element), and the height of the recursion tree can reach <span class="arithmatex">\(n - 1\)</span>, requiring <span class="arithmatex">\(O(n)\)</span> stack frame space.</p>
<p><strong>Under certain inputs, quick sort may occupy more space</strong>. For example, consider a completely ordered input array. Let the length of the sub-array in the recursion be <span class="arithmatex">\(m\)</span>. In each round of pivot partitioning, a left sub-array of length <span class="arithmatex">\(0\)</span> and a right sub-array of length <span class="arithmatex">\(m - 1\)</span> are produced. This means that the problem size is reduced by only one element per recursive call, resulting in a very small reduction at each level of recursion.
As a result, the height of the recursion tree can reach <span class="arithmatex">\(n 1\)</span> , which requires <span class="arithmatex">\(O(n)\)</span> of stack frame space.</p>
<p>To prevent the accumulation of stack frame space, we can compare the lengths of the two sub-arrays after each round of pivot sorting, <strong>and only recursively sort the shorter sub-array</strong>. Since the length of the shorter sub-array will not exceed <span class="arithmatex">\(n / 2\)</span>, this method ensures that the recursion depth does not exceed <span class="arithmatex">\(\log n\)</span>, thus optimizing the worst space complexity to <span class="arithmatex">\(O(\log n)\)</span>. The code is as follows:</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">
+7 -7
View File
@@ -3584,14 +3584,14 @@
<!-- Page content -->
<h1 id="1110-radix-sort">11.10 &nbsp; Radix sort<a class="headerlink" href="#1110-radix-sort" title="Permanent link">&para;</a></h1>
<p>The previous section introduced counting sort, which is suitable for scenarios where the data volume <span class="arithmatex">\(n\)</span> is large but the data range <span class="arithmatex">\(m\)</span> is small. Suppose we need to sort <span class="arithmatex">\(n = 10^6\)</span> student IDs, where each ID is an <span class="arithmatex">\(8\)</span>-digit number. This means the data range <span class="arithmatex">\(m = 10^8\)</span> is very large, requiring a significant amount of memory space for counting sort, while radix sort can avoid this situation.</p>
<p><u>Radix sort</u> shares the core idea with counting sort, which also sorts by counting the frequency of elements. Building on this, radix sort utilizes the progressive relationship between the digits of numbers, sorting each digit in turn to achieve the final sorted order.</p>
<p>The previous section introduced counting sort, which is suitable for scenarios where the data size <span class="arithmatex">\(n\)</span> is large but the data range <span class="arithmatex">\(m\)</span> is small. Suppose we need to sort <span class="arithmatex">\(n = 10^6\)</span> student IDs, where each ID is an <span class="arithmatex">\(8\)</span>-digit number. This means the data range <span class="arithmatex">\(m = 10^8\)</span> is very large. Using counting sort in this case would require significant memory space. Radix sort can avoid this situation.</p>
<p><u>Radix sort</u> shares the same core concept as counting sort, which also sorts by counting the frequency of elements. Meanwhile, radix sort builds upon this by utilizing the progressive relationship between the digits of numbers. It processes and sorts the digits one at a time, achieving the final sorted order.</p>
<h2 id="11101-algorithm-process">11.10.1 &nbsp; Algorithm process<a class="headerlink" href="#11101-algorithm-process" title="Permanent link">&para;</a></h2>
<p>Taking the student ID data as an example, assuming the least significant digit is the <span class="arithmatex">\(1^{st}\)</span> and the most significant is the <span class="arithmatex">\(8^{th}\)</span>, the radix sort process is illustrated in Figure 11-18.</p>
<p>Taking the student ID data as an example, assume the least significant digit is the <span class="arithmatex">\(1^{st}\)</span> and the most significant is the <span class="arithmatex">\(8^{th}\)</span>, the radix sort process is illustrated in Figure 11-18.</p>
<ol>
<li>Initialize digit <span class="arithmatex">\(k = 1\)</span>.</li>
<li>Perform "counting sort" on the <span class="arithmatex">\(k^{th}\)</span> digit of the student IDs. After completion, the data will be sorted from smallest to largest based on the <span class="arithmatex">\(k^{th}\)</span> digit.</li>
<li>Increment <span class="arithmatex">\(k\)</span> by <span class="arithmatex">\(1\)</span>, then return to step <code>2.</code> and continue iterating until all digits have been sorted, then the process ends.</li>
<li>Increment <span class="arithmatex">\(k\)</span> by <span class="arithmatex">\(1\)</span>, then return to step <code>2.</code> and continue iterating until all digits have been sorted, at which point the process ends.</li>
</ol>
<p><a class="glightbox" href="../radix_sort.assets/radix_sort_overview.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Radix sort algorithm process" class="animation-figure" src="../radix_sort.assets/radix_sort_overview.png" /></a></p>
<p align="center"> Figure 11-18 &nbsp; Radix sort algorithm process </p>
@@ -3840,14 +3840,14 @@ x_k = \lfloor\frac{x}{d^{k-1}}\rfloor \bmod d
</div>
<div class="admonition question">
<p class="admonition-title">Why start sorting from the least significant digit?</p>
<p>In consecutive sorting rounds, the result of a later round will override the result of an earlier round. For example, if the result of the first round is <span class="arithmatex">\(a &lt; b\)</span> and the result of the second round is <span class="arithmatex">\(a &gt; b\)</span>, the result of the second round will replace the first round's result. Since the significance of higher digits is greater than that of lower digits, it makes sense to sort lower digits before higher digits.</p>
<p>In consecutive sorting rounds, the result of a later round will override the result of an earlier round. For example, if the result of the first round is <span class="arithmatex">\(a &lt; b\)</span> and the second round is <span class="arithmatex">\(a &gt; b\)</span>, the second round's result will replace the first round's result. Since higher-order digits take precedence over lower-order digits, it makes sense to sort the lower digits before the higher digits.</p>
</div>
<h2 id="11102-algorithm-characteristics">11.10.2 &nbsp; Algorithm characteristics<a class="headerlink" href="#11102-algorithm-characteristics" title="Permanent link">&para;</a></h2>
<p>Compared to counting sort, radix sort is suitable for larger numerical ranges, <strong>but it assumes that the data can be represented in a fixed number of digits, and the number of digits should not be too large</strong>. For example, floating-point numbers are not suitable for radix sort, as their digit count <span class="arithmatex">\(k\)</span> may be large, potentially leading to a time complexity <span class="arithmatex">\(O(nk) \gg O(n^2)\)</span>.</p>
<p>Compared to counting sort, radix sort is suitable for larger numerical ranges, <strong>but it assumes that the data can be represented in a fixed number of digits, and the number of digits should not be too large</strong>. For example, floating-point numbers are unsuitable for radix sort, as their digit count <span class="arithmatex">\(k\)</span> may be large, potentially leading to a time complexity <span class="arithmatex">\(O(nk) \gg O(n^2)\)</span>.</p>
<ul>
<li><strong>Time complexity is <span class="arithmatex">\(O(nk)\)</span>, non-adaptive sorting</strong>: Assuming the data size is <span class="arithmatex">\(n\)</span>, the data is in base <span class="arithmatex">\(d\)</span>, and the maximum number of digits is <span class="arithmatex">\(k\)</span>, then sorting a single digit takes <span class="arithmatex">\(O(n + d)\)</span> time, and sorting all <span class="arithmatex">\(k\)</span> digits takes <span class="arithmatex">\(O((n + d)k)\)</span> time. Generally, both <span class="arithmatex">\(d\)</span> and <span class="arithmatex">\(k\)</span> are relatively small, leading to a time complexity approaching <span class="arithmatex">\(O(n)\)</span>.</li>
<li><strong>Space complexity is <span class="arithmatex">\(O(n + d)\)</span>, non-in-place sorting</strong>: Like counting sort, radix sort relies on arrays <code>res</code> and <code>counter</code> of lengths <span class="arithmatex">\(n\)</span> and <span class="arithmatex">\(d\)</span> respectively.</li>
<li><strong>Stable sorting</strong>: When counting sort is stable, radix sort is also stable; if counting sort is unstable, radix sort cannot guarantee a correct sorting outcome.</li>
<li><strong>Stable sorting</strong>: When counting sort is stable, radix sort is also stable; if counting sort is unstable, radix sort cannot ensure a correct sorting order.</li>
</ul>
<!-- Source file information -->
+4 -4
View File
@@ -3566,14 +3566,14 @@
<!-- Page content -->
<h1 id="112-selection-sort">11.2 &nbsp; Selection sort<a class="headerlink" href="#112-selection-sort" title="Permanent link">&para;</a></h1>
<p><u>Selection sort</u> works on a very simple principle: it starts a loop where each iteration selects the smallest element from the unsorted interval and moves it to the end of the sorted interval.</p>
<p>Suppose the length of the array is <span class="arithmatex">\(n\)</span>, the algorithm flow of selection sort is as shown in Figure 11-2.</p>
<p><u>Selection sort</u> works on a very simple principle: it uses a loop where each iteration selects the smallest element from the unsorted interval and moves it to the end of the sorted section.</p>
<p>Suppose the length of the array is <span class="arithmatex">\(n\)</span>, the steps of selection sort is shown in Figure 11-2.</p>
<ol>
<li>Initially, all elements are unsorted, i.e., the unsorted (index) interval is <span class="arithmatex">\([0, n-1]\)</span>.</li>
<li>Select the smallest element in the interval <span class="arithmatex">\([0, n-1]\)</span> and swap it with the element at index <span class="arithmatex">\(0\)</span>. After this, the first element of the array is sorted.</li>
<li>Select the smallest element in the interval <span class="arithmatex">\([1, n-1]\)</span> and swap it with the element at index <span class="arithmatex">\(1\)</span>. After this, the first two elements of the array are sorted.</li>
<li>Continue in this manner. After <span class="arithmatex">\(n - 1\)</span> rounds of selection and swapping, the first <span class="arithmatex">\(n - 1\)</span> elements are sorted.</li>
<li>The only remaining element is necessarily the largest element and does not need sorting, thus the array is sorted.</li>
<li>The only remaining element is subsequently the largest element and does not need sorting, thus the array is sorted.</li>
</ol>
<div class="tabbed-set tabbed-alternate" data-tabs="1:11"><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" /><div class="tabbed-labels"><label for="__tabbed_1_1">&lt;1&gt;</label><label for="__tabbed_1_2">&lt;2&gt;</label><label for="__tabbed_1_3">&lt;3&gt;</label><label for="__tabbed_1_4">&lt;4&gt;</label><label for="__tabbed_1_5">&lt;5&gt;</label><label for="__tabbed_1_6">&lt;6&gt;</label><label for="__tabbed_1_7">&lt;7&gt;</label><label for="__tabbed_1_8">&lt;8&gt;</label><label for="__tabbed_1_9">&lt;9&gt;</label><label for="__tabbed_1_10">&lt;10&gt;</label><label for="__tabbed_1_11">&lt;11&gt;</label></div>
<div class="tabbed-content">
@@ -3718,7 +3718,7 @@
</div>
<h2 id="1121-algorithm-characteristics">11.2.1 &nbsp; Algorithm characteristics<a class="headerlink" href="#1121-algorithm-characteristics" title="Permanent link">&para;</a></h2>
<ul>
<li><strong>Time complexity of <span class="arithmatex">\(O(n^2)\)</span>, non-adaptive sort</strong>: There are <span class="arithmatex">\(n - 1\)</span> rounds in the outer loop, with the unsorted interval length starting at <span class="arithmatex">\(n\)</span> in the first round and decreasing to <span class="arithmatex">\(2\)</span> in the last round, i.e., the outer loops contain <span class="arithmatex">\(n\)</span>, <span class="arithmatex">\(n - 1\)</span>, <span class="arithmatex">\(\dots\)</span>, <span class="arithmatex">\(3\)</span>, <span class="arithmatex">\(2\)</span> inner loops respectively, summing up to <span class="arithmatex">\(\frac{(n - 1)(n + 2)}{2}\)</span>.</li>
<li><strong>Time complexity of <span class="arithmatex">\(O(n^2)\)</span>, non-adaptive sort</strong>: There are <span class="arithmatex">\(n - 1\)</span> iterations in the outer loop, with the length of the unsorted section starting at <span class="arithmatex">\(n\)</span> in the first iteration and decreasing to <span class="arithmatex">\(2\)</span> in the last iteration, i.e., each outer loop iterations contain <span class="arithmatex">\(n\)</span>, <span class="arithmatex">\(n - 1\)</span>, <span class="arithmatex">\(\dots\)</span>, <span class="arithmatex">\(3\)</span>, <span class="arithmatex">\(2\)</span> inner loop iterations respectively, summing up to <span class="arithmatex">\(\frac{(n - 1)(n + 2)}{2}\)</span>.</li>
<li><strong>Space complexity of <span class="arithmatex">\(O(1)\)</span>, in-place sort</strong>: Uses constant extra space with pointers <span class="arithmatex">\(i\)</span> and <span class="arithmatex">\(j\)</span>.</li>
<li><strong>Non-stable sort</strong>: As shown in Figure 11-3, an element <code>nums[i]</code> may be swapped to the right of an equal element, causing their relative order to change.</li>
</ul>
+6 -6
View File
@@ -3588,12 +3588,12 @@
<ul>
<li>Bubble sort works by swapping adjacent elements. By adding a flag to enable early return, we can optimize the best-case time complexity of bubble sort to <span class="arithmatex">\(O(n)\)</span>.</li>
<li>Insertion sort sorts each round by inserting elements from the unsorted interval into the correct position in the sorted interval. Although the time complexity of insertion sort is <span class="arithmatex">\(O(n^2)\)</span>, it is very popular in sorting small amounts of data due to relatively fewer operations per unit.</li>
<li>Quick sort is based on sentinel partitioning operations. In sentinel partitioning, it's possible to always pick the worst pivot, leading to a time complexity degradation to <span class="arithmatex">\(O(n^2)\)</span>. Introducing median or random pivots can reduce the probability of such degradation. Tail recursion can effectively reduce the recursion depth, optimizing the space complexity to <span class="arithmatex">\(O(\log n)\)</span>.</li>
<li>Quick sort is based on sentinel partitioning operations. In sentinel partitioning, it's possible to always pick the worst pivot, leading to a time complexity degradation to <span class="arithmatex">\(O(n^2)\)</span>. Introducing median or random pivots can reduce the probability of such degradation. Tail recursion effectively reduce the recursion depth, optimizing the space complexity to <span class="arithmatex">\(O(\log n)\)</span>.</li>
<li>Merge sort includes dividing and merging two phases, typically embodying the divide-and-conquer strategy. In merge sort, sorting an array requires creating auxiliary arrays, resulting in a space complexity of <span class="arithmatex">\(O(n)\)</span>; however, the space complexity for sorting a list can be optimized to <span class="arithmatex">\(O(1)\)</span>.</li>
<li>Bucket sort consists of three steps: data bucketing, sorting within buckets, and merging results. It also embodies the divide-and-conquer strategy, suitable for very large datasets. The key to bucket sort is the even distribution of data.</li>
<li>Counting sort is a special case of bucket sort, which sorts by counting the occurrences of each data point. Counting sort is suitable for large datasets with a limited range of data and requires that data can be converted to positive integers.</li>
<li>Radix sort sorts data by sorting digit by digit, requiring data to be represented as fixed-length numbers.</li>
<li>Overall, we hope to find a sorting algorithm that has high efficiency, stability, in-place operation, and adaptability. However, like other data structures and algorithms, no sorting algorithm can meet all these conditions simultaneously. In practical applications, we need to choose the appropriate sorting algorithm based on the characteristics of the data.</li>
<li>Bucket sort consists of three steps: distributing data into buckets, sorting within each bucket, and merging results in bucket order. It also embodies the divide-and-conquer strategy, suitable for very large datasets. The key to bucket sort is the even distribution of data.</li>
<li>Counting sort is a variant of bucket sort, which sorts by counting the occurrences of each data point. Counting sort is suitable for large datasets with a limited range of data and requires data conversion to positive integers.</li>
<li>Radix sort processes data by sorting it digit by digit, requiring data to be represented as fixed-length numbers.</li>
<li>Overall, we seek sorting algorithm that has high efficiency, stability, in-place operation, and adaptability. However, like other data structures and algorithms, no sorting algorithm can meet all these conditions simultaneously. In practical applications, we need to choose the appropriate sorting algorithm based on the characteristics of the data.</li>
<li>Figure 11-19 compares mainstream sorting algorithms in terms of efficiency, stability, in-place nature, and adaptability.</li>
</ul>
<p><a class="glightbox" href="../summary.assets/sorting_algorithms_comparison.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Sorting Algorithm Comparison" class="animation-figure" src="../summary.assets/sorting_algorithms_comparison.png" /></a></p>
@@ -3605,7 +3605,7 @@
<p>It can be seen that the positions of students D and C have been swapped, disrupting the orderliness of the names, which is undesirable.</p>
<p><strong>Q</strong>: Can the order of "searching from right to left" and "searching from left to right" in sentinel partitioning be swapped?</p>
<p>No, when using the leftmost element as the pivot, we must first "search from right to left" then "search from left to right". This conclusion is somewhat counterintuitive, so let's analyze the reason.</p>
<p>The last step of the sentinel partition <code>partition()</code> is to swap <code>nums[left]</code> and <code>nums[i]</code>. After the swap, the elements to the left of the pivot are all <code>&lt;=</code> the pivot, <strong>which requires that <code>nums[left] &gt;= nums[i]</code> must hold before the last swap</strong>. Suppose we "search from left to right" first, then if no element larger than the pivot is found, <strong>we will exit the loop when <code>i == j</code>, possibly with <code>nums[j] == nums[i] &gt; nums[left]</code></strong>. In other words, the final swap operation will exchange an element larger than the pivot to the left end of the array, causing the sentinel partition to fail.</p>
<p>The last step of the sentinel partition <code>partition()</code> is to swap <code>nums[left]</code> and <code>nums[i]</code>. After the swap, the elements to the left of the pivot are all <code>&lt;=</code> the pivot, <strong>which requires that <code>nums[left] &gt;= nums[i]</code> must hold before the last swap</strong>. Suppose we "search from left to right" first, and if no element larger than the pivot is found, <strong>we will exit the loop when <code>i == j</code>, possibly with <code>nums[j] == nums[i] &gt; nums[left]</code></strong>. In other words, the final swap operation will exchange an element larger than the pivot to the left end of the array, causing the sentinel partition to fail.</p>
<p>For example, given the array <code>[0, 0, 0, 0, 1]</code>, if we first "search from left to right", the array after the sentinel partition is <code>[1, 0, 0, 0, 0]</code>, which is incorrect.</p>
<p>Upon further consideration, if we choose <code>nums[right]</code> as the pivot, then exactly the opposite, we must first "search from left to right".</p>
<p><strong>Q</strong>: Regarding tail recursion optimization, why does choosing the shorter array ensure that the recursion depth does not exceed <span class="arithmatex">\(\log n\)</span>?</p>