mirror of
https://github.com/krahets/hello-algo.git
synced 2026-08-29 03:17:13 +00:00
deploy
This commit is contained in:
@@ -3602,10 +3602,10 @@
|
||||
|
||||
<!-- 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 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 Algorithm process<a class="headerlink" href="#1181-algorithm-process" title="Permanent link">¶</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 Algorithm characteristics<a class="headerlink" href="#1182-algorithm-characteristics" title="Permanent link">¶</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 How to achieve even distribution<a class="headerlink" href="#1183-how-to-achieve-even-distribution" title="Permanent link">¶</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 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 Dividing buckets based on probability distribution </p>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user