{"id":154,"date":"2026-11-13T12:32:56","date_gmt":"2026-11-13T04:32:56","guid":{"rendered":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-bucket-sort-uses-a-uniform-spread\/"},"modified":"2026-11-13T21:51:26","modified_gmt":"2026-11-13T13:51:26","slug":"how-bucket-sort-uses-a-uniform-spread","status":"publish","type":"post","link":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-bucket-sort-uses-a-uniform-spread\/","title":{"rendered":"How bucket sort uses a uniform spread"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Short answer:<\/strong> <strong>Bucket sort<\/strong> drops keys from a known numeric range into n buckets, sorts each bucket, and concatenates them. When keys are independent and uniform, the buckets stay small and the average time is <strong>\u0398(n)<\/strong>. One bucket that receives every key falls back to the inner sort, which is <strong>\u0398(n\u00b2)<\/strong> if that inner sort is insertion sort. The uniform spread is a hypothesis about the input, not a property the algorithm enforces.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the problem actually asks<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">Keys are real numbers in a half-open interval, conventionally [0, 1), or integers that have been scaled into that interval. A key x belongs in bucket floor(n\u00b7x). Inside a bucket the keys occupy a short range and can be sorted by insertion sort. Concatenating bucket 0 through bucket n\u22121 preserves global order because every key in an earlier bucket is smaller than every key in a later bucket.<\/p>\n\n\n<p class=\"wp-block-paragraph\">The average-case proof needs the keys to be drawn independently from a uniform distribution, so that the bucket sizes are balanced with high probability. A distribution that clumps breaks the balance and leaves the proof behind.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the algorithm guarantees<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">Scattering is \u0398(n), and concatenating is \u0398(n). The inner sorts together cost time proportional to the sum of squares of the bucket sizes. Under independent uniform keys that sum is \u0398(n) in expectation, so the expected running time is \u0398(n). The worst case, every key in one bucket, is the worst case of the inner sort. Extra memory is \u0398(n) for the buckets.<\/p>\n\n\n<p class=\"wp-block-paragraph\">The sort is stable when each bucket&#8217;s inner sort is stable and records are appended to a bucket in input order. Insertion sort meets that condition. The interval must be known. A key of 1.0 in a [0, 1) layout is out of range and has to be placed by an explicit rule, usually a last bucket.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>The working steps<\/strong><\/h4>\n\n<ol class=\"wp-block-list\">\n<li>Scale the keys into [0, 1), using the known minimum and maximum when the raw range is something else. Reject or clamp a key that falls outside after scaling, under an explicit rule.<\/li>\n<li>Allocate n empty buckets. Send key x to bucket floor(n\u00b7x), and to bucket n\u22121 if a scaled key would otherwise land on n.<\/li>\n<li>Sort each bucket with insertion sort.<\/li>\n<li>Write the buckets out in index order.<\/li>\n<li>If the input distribution is known to be non-uniform, change the bucket boundaries to equal-probability quantiles. Equal-width buckets are the uniform case only.<\/li>\n<\/ol>\n\n\n<p class=\"wp-block-paragraph\">Expected linear time is a statement about uniform independent keys and an inner sort whose cost is quadratic in the bucket size. Integers in a small discrete range are counting sort. Fixed-width words are radix sort. Neither needs a uniformity hypothesis.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Approaches that look simpler and fail<\/strong><\/h4>\n\n<figure class=\"wp-block-table\">\n<table>\n<thead>\n<tr>\n<th>Approach<\/th>\n<th>Why it fails when the uniform-bucket hypothesis is false<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Equal-width buckets on a skewed distribution<\/td>\n<td>A clump falls in one bucket. Expected linear time was a statement about uniform draws, and this input is the worst case of insertion sort.<\/td>\n<\/tr>\n<tr>\n<td>Skip the inner sort because the buckets look ordered<\/td>\n<td>A bucket holds an unordered subset of a narrow range. Order inside the bucket still has to be computed.<\/td>\n<\/tr>\n<tr>\n<td>Use n\u00b2 buckets to force tiny lists<\/td>\n<td>Memory and the scatter pass then dominate, and the algorithm has become a sparse counting sort with a worse constant.<\/td>\n<\/tr>\n<tr>\n<td>Apply it to keys that only support comparison<\/td>\n<td>The bucket index is an arithmetic function of the key and the range. A comparison oracle does not provide one.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n<p class=\"wp-block-paragraph\">Bounded integer keys belong to counting sort and radix sort, which have worst-case linear bounds in n plus the digit parameters. A comparison sort is the fallback when no range and no distribution are known.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>A working example<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">This Python fragment scatters values in [0, 1) into equal buckets, sorts each bucket, and concatenates them. It prints <code>0.12 0.17 0.21 0.35 0.78<\/code>.<\/p>\n\n<pre><code class=\"language-python\">values = [0.78, 0.17, 0.21, 0.35, 0.12]\nn = len(values)\nbuckets = [[] for _ in range(n)]\nfor v in values:\n    buckets[min(n - 1, int(v * n))].append(v)\nfor bucket in buckets:\n    bucket.sort()\nout = [v for bucket in buckets for v in bucket]\nassert out == sorted(values)\nprint(*(f&quot;{v:.2f}&quot; for v in out))\n<\/code><\/pre>\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Where it is used<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">Bucket sort is the distribution sort for keys that are spread evenly, such as normalized scores or uniform random floats. Uneven keys dump into one bucket and the method falls back to whatever sorts that bucket. Integers in a small range use counting sort instead.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>When you can drop this algorithm<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">Drop bucket sort when the distribution may clump, unless the bucket boundaries are rebuilt from real quantiles and that cost is included. Use radix sort for machine words and mergesort or heapsort for general keys. Keep bucket sort for numeric keys that really are spread evenly across a known interval.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Why this stays published<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">We publish this so a search for bucket sort returns the uniform hypothesis that the expected linear bound depends on. <strong>Bucket sort expects linear time when keys are spread uniformly across n buckets, and it degrades to the inner sort when they are not.<\/strong><\/p>\n\n\n<p class=\"wp-block-paragraph\">\u672c\u6587\u7531 HoHo \u8207 AI \u5354\u4f5c\u6574\u7406\uff0c\u6700\u5f8c\u66f4\u65b0\u65bc 2026 \u5e74 11 \u6708 13 \u65e5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Short answer: Bucket sort drops keys from a known numeric range into n buckets, sorts each bucket, and concatenates them. When keys are independent and uniform, the buckets stay small and the average time is \u0398(n). One bucket that receives every key falls back to the inner sort, which&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30,31],"tags":[],"class_list":["post-154","post","type-post","status-publish","format-standard","hentry","category-algorithms","category-sorting"],"_links":{"self":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/154","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/comments?post=154"}],"version-history":[{"count":2,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/154\/revisions"}],"predecessor-version":[{"id":553,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/154\/revisions\/553"}],"wp:attachment":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/media?parent=154"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/categories?post=154"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/tags?post=154"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}