{"id":4673,"date":"2024-08-23T15:50:53","date_gmt":"2024-08-23T15:50:53","guid":{"rendered":"https:\/\/msgprogramator.sk\/?p=4673"},"modified":"2026-03-03T09:50:35","modified_gmt":"2026-03-03T09:50:35","slug":"bucket-sort","status":"publish","type":"post","link":"https:\/\/msgprogramator.sk\/en\/bucket-sort\/","title":{"rendered":"Bucket sort algorithm Java"},"content":{"rendered":"<p>Sorting algorithms are used to reorder the elements of an array or list in ascending or descending numerical or lexicographic order. Sorting is very important for the optimal performance of other algorithms that require sorted input data.<\/p>\n<p>There are numerous sorting algorithms available. The selection of an appropriate algorithm depends on factors such as the size and characteristics of the input data, available memory, and time and space complexity.<\/p>\n<p>To make your choice easier, in our series we will introduce the most well-known data sorting algorithms, explain their principles, advantages and disadvantages, and program them in Java. Today we&#8217;re going to look at <strong>bucket sort<\/strong>, also known as <strong>bin sort<\/strong>.<\/p>\n<p>So far, we have discussed the following:<\/p>\n<ul>\n<li><a href=\"https:\/\/msgprogramator.sk\/en\/sorting-algorithms\/\" target=\"_blank\" rel=\"noopener\">Sorting algorithms<\/a>: Introduction to data sorting<\/li>\n<li><a href=\"https:\/\/msgprogramator.sk\/en\/bubble-sort\/\" target=\"_blank\" rel=\"noopener\">Bubble sort<\/a><\/li>\n<li><a href=\"https:\/\/msgprogramator.sk\/en\/comb-sort\/\" target=\"_blank\" rel=\"noopener\">Comb sort<\/a><\/li>\n<li><a href=\"https:\/\/msgprogramator.sk\/en\/big-o-notation\/\" target=\"_blank\" rel=\"noopener\">Big O notation<\/a><\/li>\n<li><a href=\"https:\/\/msgprogramator.sk\/en\/selection-sort\/\" target=\"_blank\" rel=\"noopener\">Selection sort<\/a><\/li>\n<li><a href=\"https:\/\/msgprogramator.sk\/en\/insertion-sort\/\" target=\"_blank\" rel=\"noopener\">Insertion sort<\/a><\/li>\n<li><a href=\"https:\/\/msgprogramator.sk\/en\/counting-sort\/\" target=\"_blank\" rel=\"noopener\">Counting sort<\/a><\/li>\n<li><a href=\"https:\/\/msgprogramator.sk\/en\/heap-sort\/\" target=\"_blank\" rel=\"noopener\">Heap sort<\/a><\/li>\n<li><a href=\"https:\/\/msgprogramator.sk\/en\/merge-sort\/\" target=\"_blank\" rel=\"noopener\">Merge sort<\/a><\/li>\n<li><a href=\"https:\/\/msgprogramator.sk\/en\/quick-sort\/\" target=\"_blank\" rel=\"noopener\">Quick sort<\/a><\/li>\n<\/ul>\n<h2>Bucket sort definition<\/h2>\n<p>Bucket (Bin) sort is a special sorting technique that consists in dividing the elements of the input vector into different groups (buckets or bins). These groups are formed on the basis of a <a href=\"https:\/\/www.investopedia.com\/terms\/u\/uniform-distribution.asp\" target=\"_blank\" rel=\"nofollow noopener\">uniform distribution<\/a>. The groups are then individually sorted by an arbitrary sorting algorithm and the output sorted output is the union of the results of the individual buckets. The computational complexity of the algorithm then depends on the algorithm used to sort individual buckets, the number of buckets created and on how uniformly the values are distributed in the input.<\/p>\n<h2>Bucket sort explained<\/h2>\n<p>If we need to sort a large amount of data that has the property that it is evenly distributed (e.g. order numbers), by dividing it into subgroups we can reduce the amount of comparisons between elements, which will help us to reduce the sorting time. Once the data is sorted, we can keep it in buckets, and when we add new elements to the buckets, we can sort only the buckets to which the new element has been added. Sorted data is also easier and faster to sort if we use a suitable algorithm, such as <a href=\"https:\/\/msgprogramator.sk\/en\/insertion-sort\/\" target=\"_blank\" rel=\"noopener\">insertion sort<\/a>. Partitioning into multiple buckets also allows us to use parallel sorting and data processing techniques.<\/p>\n<p>The basic principle of the bucket sort algorithm is as follows:<\/p>\n<ol start=\"1\">\n<li><strong>Determine bucket count<\/strong> &#8211; By analyzing data range once, we can determine the maximum value<\/li>\n<li><strong> Create empty buckets (typically using an array of lists)<\/strong><\/li>\n<li><strong> Distribute elements into buckets<\/strong><br \/>\nThe most appropriate way is to <strong><em>scatter<\/em><\/strong> them using a simple hash function.<\/li>\n<li><strong> Sort each bucket individually<\/strong><\/li>\n<li><strong> <em>Gather<\/em> the results from each bucket in the correct order<\/strong><\/li>\n<\/ol>\n<h2>Bucket sort animation, visualization, gif<\/h2>\n<figure id=\"attachment_4212\" aria-describedby=\"caption-attachment-4212\" style=\"width: 890px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-4211 size-full\" src=\"https:\/\/msgprogramator.sk\/wp-content\/uploads\/2024\/08\/bucket-sort-ilustracia-890-740.webp\" alt=\"Visual representation of the Bucket-Sort algorithm.\" width=\"890\" height=\"740\" srcset=\"https:\/\/msgprogramator.sk\/wp-content\/uploads\/2024\/08\/bucket-sort-ilustracia-890-740.webp 890w, https:\/\/msgprogramator.sk\/wp-content\/uploads\/2024\/08\/bucket-sort-ilustracia-890-740-300x249.webp 300w, https:\/\/msgprogramator.sk\/wp-content\/uploads\/2024\/08\/bucket-sort-ilustracia-890-740-768x639.webp 768w\" sizes=\"auto, (max-width: 890px) 100vw, 890px\" \/><figcaption id=\"caption-attachment-4212\" class=\"wp-caption-text\">Visual representation of the Bucket-Sort algorithm.<\/figcaption><\/figure>\n<h2>Bucket sort advantages<\/h2>\n<ul>\n<li>Relatively efficient for data with a uniform distribution: decimal numbers with a range of 0.0 to 1.0, or integers in a narrow range<\/li>\n<li>Stable if a stable sorting algorithm is used to sort the data for groups<\/li>\n<li>It can be parallelized to spread the computational load across multiple processors.<\/li>\n<\/ul>\n<h2>Bucket sort disadvantages<\/h2>\n<ul>\n<li>Requires additional memory space to create buckets<\/li>\n<li>An inappropriately chosen variance function can significantly increase the time complexity.<\/li>\n<\/ul>\n<h2>Bucket sort time complexity<\/h2>\n<table width=\"536\">\n<thead>\n<tr>\n<th rowspan=\"2\" width=\"94\"><strong>Algorithm<\/strong><\/th>\n<th rowspan=\"2\" width=\"84\"><strong>Method<\/strong><\/th>\n<th colspan=\"3\" width=\"208\"><strong>Time Complexity<\/strong><\/th>\n<th rowspan=\"2\" width=\"82\"><strong>Memory<\/strong><\/th>\n<th rowspan=\"2\" width=\"69\"><strong>Stable<\/strong><\/th>\n<\/tr>\n<tr>\n<th width=\"73\"><strong>worst<\/strong><\/th>\n<th width=\"63\"><strong>average<\/strong><\/th>\n<th width=\"71\"><strong>best<\/strong><\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td width=\"94\">Bucket sort<\/td>\n<td width=\"84\">Distribution<\/td>\n<td width=\"73\">O(n\u00b2)<\/td>\n<td width=\"63\">O(n + k)<\/td>\n<td width=\"71\">O(n + k)<\/td>\n<td width=\"82\">O(n + k)<\/td>\n<td width=\"69\">yes\/no<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Variable n = number of array elements, k = number of buckets<\/p>\n<h3>Bucket sort worst case<\/h3>\n<p>In the worst case scenario, all elements end up in the same bucket in reverse order. This may be the result of an inappropriate data distribution for this algorithm, or an incorrectly implemented variance function. In this case, the time complexity approaches O(n\u00b2).<\/p>\n<h2>Bucket Sort implementation, Java code<\/h2>\n<p>Now we will show the implementation of the bucket sort algorithm in Java.<\/p>\n<p><strong><u>BucketSort.java<\/u><\/strong><\/p>\n<pre><code class=\"language-java\" data-line=\"\">package sorting;\n\nimport java.util.ArrayList;\nimport java.util.Collections;\n\npublic class BucketSort {\n    public void sort(float[] data)\n    {\n        \/\/ Set optimal bucket count here\n        int bucketCount = 10;\n        ArrayList&lt;Float&gt;[] buckets = new ArrayList[bucketCount];\n\n        \/\/ Create empty buckets\n        for (int i = 0; i &lt; bucketCount; i++) {\n            buckets[i] = new ArrayList&lt;&gt;();\n        }\n\n        \/\/ Scatter elements into buckets using appropriate hash function\n        for (float f : data) {\n            int bucketIndex = (int) Math.floor(f * 10);\n            buckets[bucketIndex].add(f);\n        }\n\n        System.out.println(&quot;After distribution:&quot;);\n        for (int i = 0; i &lt; bucketCount; i++) {\n            printBucket(i, buckets[i]);\n        }\n\n        \/\/ Sort all the buckets\n        for (int i = 0; i &lt; bucketCount; i++) {\n            Collections.sort(buckets[i]);\n        }\n\n        System.out.println(&quot;After sorting individual buckets:&quot;);\n        for (int i = 0; i &lt; bucketCount; i++) {\n            printBucket(i, buckets[i]);\n        }\n\n        \/\/ Concatenate buckets elements to get sorted data\n        int j = 0;\n        for (int i = 0; i &lt; bucketCount; i++) {\n            for (float num : buckets[i]) {\n                data[j++] = num;\n            }\n        }\n    }\n\n    public void printBucket(int id, ArrayList&lt;Float&gt; data)\n    {\n        System.out.println(&quot;Bucket &quot; + id + &quot; -&gt; &quot; + data);\n    }\n\n    \/\/ Function to print an array\n    public void printArray(float[] data)\n    {\n        for (int i = 0; i &lt; data.length; i++)\n            System.out.print(data[i] + &quot; &quot;);\n    }\n}<\/code><\/pre>\n<p><strong><u>Main.java<\/u><\/strong><\/p>\n<pre><code class=\"language-java\" data-line=\"\">import sorting.BucketSort;\n\npublic class Main {\n    public static void main(String[] args) {\n        float[] dataToSort = { 0.88f, 0.16f, 0.39f, 0.26f, 0.82f, 0.94f, 0.21f, 0.12f, 0.23f, 0.68f };\n        BucketSort bucketSort = new BucketSort();\n        System.out.print(&quot;Input: &quot;);\n        bucketSort.printArray(dataToSort);\n        System.out.println();\n        bucketSort.sort(dataToSort);\n        System.out.print(&quot;Sorted: &quot;);\n        bucketSort.printArray(dataToSort);\n    }\n}<\/code><\/pre>\n<p>The output of this bucket sort Java example is:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-4220 size-full\" src=\"https:\/\/msgprogramator.sk\/wp-content\/uploads\/2024\/08\/vystup-z-prikladu-bucket-sort-670-820.webp\" alt=\"Bucket sort implementation, Java code\" width=\"670\" height=\"820\" srcset=\"https:\/\/msgprogramator.sk\/wp-content\/uploads\/2024\/08\/vystup-z-prikladu-bucket-sort-670-820.webp 670w, https:\/\/msgprogramator.sk\/wp-content\/uploads\/2024\/08\/vystup-z-prikladu-bucket-sort-670-820-245x300.webp 245w\" sizes=\"auto, (max-width: 670px) 100vw, 670px\" \/><\/p>\n<p>We have prepared the files with the above example as code that you can run directly in Java. Download the <a href=\"https:\/\/msgprogramator.sk\/wp-content\/uploads\/2024\/08\/BucketSort.zip\" target=\"_blank\" rel=\"noopener\">Java <strong>BucketSort <\/strong> code<\/a>.<\/p>\n<p>If you&#8217;re a <a href=\"https:\/\/msg-life.sk\/en\/jobs\/java-programmer-senior\/\" target=\"_blank\" rel=\"noopener\">Java developer<\/a> looking for a job, check out our <a href=\"https:\/\/msg-life.sk\/en\/benefits\/\" target=\"_blank\" rel=\"noopener\">employee benefits<\/a> and respond to our <a href=\"https:\/\/msg-life.sk\/en\/jobs\/\" target=\"_blank\" rel=\"noopener\">job offers<\/a>!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article you will learn what bucket sort is, its principle, advantages, disadvantages, visualization and example Java code.<\/p>\n","protected":false},"author":14,"featured_media":4215,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[57],"tags":[],"class_list":["post-4673","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java"],"acf":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/msgprogramator.sk\/en\/wp-json\/wp\/v2\/posts\/4673","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/msgprogramator.sk\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/msgprogramator.sk\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/msgprogramator.sk\/en\/wp-json\/wp\/v2\/users\/14"}],"replies":[{"embeddable":true,"href":"https:\/\/msgprogramator.sk\/en\/wp-json\/wp\/v2\/comments?post=4673"}],"version-history":[{"count":6,"href":"https:\/\/msgprogramator.sk\/en\/wp-json\/wp\/v2\/posts\/4673\/revisions"}],"predecessor-version":[{"id":9738,"href":"https:\/\/msgprogramator.sk\/en\/wp-json\/wp\/v2\/posts\/4673\/revisions\/9738"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/msgprogramator.sk\/en\/wp-json\/wp\/v2\/media\/4215"}],"wp:attachment":[{"href":"https:\/\/msgprogramator.sk\/en\/wp-json\/wp\/v2\/media?parent=4673"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/msgprogramator.sk\/en\/wp-json\/wp\/v2\/categories?post=4673"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/msgprogramator.sk\/en\/wp-json\/wp\/v2\/tags?post=4673"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}