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>