mirror of
https://github.com/krahets/hello-algo.git
synced 2026-08-18 22:40:59 +00:00
deploy
This commit is contained in:
@@ -4674,7 +4674,7 @@
|
||||
|
||||
<!-- Page content -->
|
||||
<h1 id="118-bucket-sort">11.8 Bucket Sort<a class="headerlink" href="#118-bucket-sort" title="Permanent link">¶</a></h1>
|
||||
<p>The sorting algorithms discussed earlier are all comparison-based sorting algorithms, which sort by comparing the relative order of elements. The time complexity of such algorithms cannot beat <span class="arithmatex">\(O(n \log n)\)</span>. Next, we will explore several non-comparison sorting algorithms, whose time complexity can be linear.</p>
|
||||
<p>The sorting algorithms discussed earlier are all comparison-based sorting algorithms, which sort by comparing the relative order of elements. The worst-case time complexity of such algorithms has a lower bound of <span class="arithmatex">\(\Omega(n \log n)\)</span>. Next, we will explore several non-comparison sorting algorithms, whose time complexity can be linear.</p>
|
||||
<p><u>Bucket sort</u> is a typical application of the divide-and-conquer strategy. It works by creating a sequence of ordered buckets, each corresponding to a data range, and distributing the data evenly among them. The elements within each bucket are then sorted separately. Finally, all buckets are merged in order.</p>
|
||||
<h2 id="1181-algorithm-flow">11.8.1 Algorithm Flow<a class="headerlink" href="#1181-algorithm-flow" title="Permanent link">¶</a></h2>
|
||||
<p>Consider an array of length <span class="arithmatex">\(n\)</span>, whose elements are floating-point numbers in the range <span class="arithmatex">\([0, 1)\)</span>. The flow of bucket sort is shown in Figure 11-13.</p>
|
||||
|
||||
@@ -4932,7 +4932,7 @@
|
||||
</div>
|
||||
<h2 id="1121-algorithm-characteristics">11.2.1 Algorithm Characteristics<a class="headerlink" href="#1121-algorithm-characteristics" title="Permanent link">¶</a></h2>
|
||||
<ul>
|
||||
<li><strong>Time complexity <span class="arithmatex">\(O(n^2)\)</span>, non-adaptive sorting</strong>: The outer loop has <span class="arithmatex">\(n - 1\)</span> rounds in total. The length of the unsorted interval in the first round is <span class="arithmatex">\(n\)</span>, and the length of the unsorted interval in the last round is <span class="arithmatex">\(2\)</span>. That is, the rounds of the outer loop contain inner loops with <span class="arithmatex">\(n\)</span>, <span class="arithmatex">\(n - 1\)</span>, <span class="arithmatex">\(\dots\)</span>, <span class="arithmatex">\(3\)</span>, and <span class="arithmatex">\(2\)</span> iterations, summing to <span class="arithmatex">\(\frac{(n - 1)(n + 2)}{2}\)</span>.</li>
|
||||
<li><strong>Time complexity <span class="arithmatex">\(O(n^2)\)</span>, non-adaptive sorting</strong>: The outer loop has <span class="arithmatex">\(n - 1\)</span> rounds in total. The inner loop runs <span class="arithmatex">\(n - 1\)</span> times in the first round and <span class="arithmatex">\(1\)</span> time in the last round. Thus, it runs <span class="arithmatex">\(n - 1\)</span>, <span class="arithmatex">\(n - 2\)</span>, <span class="arithmatex">\(\dots\)</span>, <span class="arithmatex">\(2\)</span>, and <span class="arithmatex">\(1\)</span> times across the rounds, summing to <span class="arithmatex">\(\frac{n(n - 1)}{2}\)</span>.</li>
|
||||
<li><strong>Space complexity <span class="arithmatex">\(O(1)\)</span>, in-place sorting</strong>: Pointers <span class="arithmatex">\(i\)</span> and <span class="arithmatex">\(j\)</span> use a constant amount of extra space.</li>
|
||||
<li><strong>Unstable sorting</strong>: As shown in Figure 11-3, element <code>nums[i]</code> may be swapped to the right of an element equal to it, causing a change in their relative order.</li>
|
||||
</ul>
|
||||
|
||||
@@ -4680,7 +4680,7 @@
|
||||
<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">'E'</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> can utilize the existing order information in the input data to reduce the amount of computation, achieving better time efficiency. The best-case time complexity of adaptive sorting algorithms is typically better than the average time complexity.</p>
|
||||
<p><strong>Comparison-based or non-comparison</strong>: <u>Comparison-based sorting</u> relies on comparison operators (<span class="arithmatex">\(<\)</span>, <span class="arithmatex">\(=\)</span>, <span class="arithmatex">\(>\)</span>) to determine the relative order of elements, thereby sorting the entire array, with a theoretical optimal time complexity of <span class="arithmatex">\(O(n \log n)\)</span>. <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 limited.</p>
|
||||
<p><strong>Comparison-based or non-comparison</strong>: <u>Comparison-based sorting</u> relies on comparison operators (<span class="arithmatex">\(<\)</span>, <span class="arithmatex">\(=\)</span>, <span class="arithmatex">\(>\)</span>) to determine the relative order of elements, thereby sorting the entire array. Its worst-case time complexity has a lower bound of <span class="arithmatex">\(\Omega(n \log n)\)</span>. <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 limited.</p>
|
||||
<h2 id="1112-ideal-sorting-algorithm">11.1.2 Ideal Sorting Algorithm<a class="headerlink" href="#1112-ideal-sorting-algorithm" title="Permanent link">¶</a></h2>
|
||||
<p><strong>Fast, in-place, stable, adaptive, and broadly applicable</strong>. Clearly, no sorting algorithm has been discovered to date that combines all of these characteristics. 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 examine various sorting algorithms and analyze their advantages and disadvantages based on the evaluation dimensions above.</p>
|
||||
|
||||
Reference in New Issue
Block a user