This commit is contained in:
krahets
2024-05-01 07:30:15 +08:00
parent 85f0bc4ed1
commit d246e08cc6
68 changed files with 220 additions and 220 deletions
+3 -3
View File
@@ -3612,7 +3612,7 @@
<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>
<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 the Figure 11-13 .</p>
<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>
<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>
@@ -4035,12 +4035,12 @@
<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 the 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>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><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 the 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>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><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>