This commit is contained in:
krahets
2024-07-30 16:54:47 +08:00
parent 8fb4fdc14c
commit 55b91eb967
140 changed files with 426 additions and 1278 deletions
+1 -2
View File
@@ -3756,8 +3756,7 @@
<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>
<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.</li>
<li><strong>Adaptive sorting</strong>: 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 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>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>
</ul>
+1 -1
View File
@@ -3918,7 +3918,7 @@
</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>, 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>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>Non-stable sorting</strong>: In the final step of pivot partitioning, the pivot may be swapped to the right of equal elements.</li>
</ul>
@@ -3618,11 +3618,10 @@
<a id="__codelineno-0-15" name="__codelineno-0-15" href="#__codelineno-0-15"></a><span class="w"> </span><span class="o">(</span><span class="s1">&#39;C&#39;</span>,<span class="w"> </span><span class="m">21</span><span class="o">)</span>
<a id="__codelineno-0-16" name="__codelineno-0-16" href="#__codelineno-0-16"></a><span class="w"> </span><span class="o">(</span><span class="s1">&#39;E&#39;</span>,<span class="w"> </span><span class="m">23</span><span class="o">)</span>
</code></pre></div>
<p><strong>Adaptability</strong>: <u>Adaptive sorting</u> has a time complexity that depends on the input data, i.e., the best time complexity, worst time complexity, and average time complexity are not exactly equal.</p>
<p>Adaptability needs to be assessed according to the specific situation. If the worst time complexity is worse than the average, it suggests that the performance of the sorting algorithm might deteriorate under certain data, hence it is seen as a negative attribute; whereas, if the best time complexity is better than the average, it is considered a positive attribute.</p>
<p><strong>Adaptability</strong>: <u>Adaptive sorting</u> leverages existing order information within the input data to reduce computational effort, achieving more optimal time efficiency. The best-case time complexity of adaptive sorting algorithms is typically better than their average-case time complexity.</p>
<p><strong>Comparison-based</strong>: <u>Comparison-based sorting</u> relies on comparison operators (<span class="arithmatex">\(&lt;\)</span>, <span class="arithmatex">\(=\)</span>, <span class="arithmatex">\(&gt;\)</span>) to determine the relative order of elements and thus sort the entire array, with the theoretical optimal time complexity being <span class="arithmatex">\(O(n \log n)\)</span>. Meanwhile, <u>non-comparison sorting</u> does not use comparison operators and can achieve a time complexity of <span class="arithmatex">\(O(n)\)</span>, but its versatility is relatively poor.</p>
<h2 id="1112-ideal-sorting-algorithm">11.1.2 &nbsp; Ideal sorting algorithm<a class="headerlink" href="#1112-ideal-sorting-algorithm" title="Permanent link">&para;</a></h2>
<p><strong>Fast execution, in-place, stable, positively adaptive, and versatile</strong>. Clearly, no sorting algorithm that combines all these features has been found to date. Therefore, when selecting a sorting algorithm, it is necessary to decide based on the specific characteristics of the data and the requirements of the problem.</p>
<p><strong>Fast execution, in-place, stable, adaptive, and versatile</strong>. Clearly, no sorting algorithm that combines all these features has been found to date. Therefore, when selecting a sorting algorithm, it is necessary to decide based on the specific characteristics of the data and the requirements of the problem.</p>
<p>Next, we will learn about various sorting algorithms together and analyze the advantages and disadvantages of each based on the above evaluation dimensions.</p>
<!-- Source file information -->
Binary file not shown.

Before

Width:  |  Height:  |  Size: 61 KiB

After

Width:  |  Height:  |  Size: 58 KiB

+1 -1
View File
@@ -3600,7 +3600,7 @@
<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 positive 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>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>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>