{"id":25194,"date":"2017-10-15T14:53:36","date_gmt":"2017-10-15T09:23:36","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=25194"},"modified":"2017-10-15T14:53:36","modified_gmt":"2017-10-15T09:23:36","slug":"bucket-sort","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/bucket-sort\/","title":{"rendered":"Bucket Sort"},"content":{"rendered":"<p>Bucket sort is mainly useful when input is uniformly distributed over a range. For example, consider the following problem. <span id=\"more-128013\"><\/span><br \/>\nSort a large set of floating point numbers which are in range from 0.0 to 1.0 and are uniformly distributed across the range. How do we sort the numbers efficiently?<\/p>\n<p>A simple way is to apply a comparison based sorting algorithm. The <a href=\"http:\/\/www.geeksforgeeks.org\/lower-bound-on-comparison-based-sorting-algorithms\/\" target=\"_blank\" rel=\"noopener noreferrer\">lower bound for Comparison based sorting algorithm<\/a> (Merge Sort, Heap Sort, Quick-Sort .. etc) is \u03a9(n Log n), i.e., they cannot do better than nLogn.<br \/>\nCan we sort the array in linear time? <a href=\"http:\/\/www.geeksforgeeks.org\/counting-sort\/\" target=\"_blank\" rel=\"noopener noreferrer\">Counting sort<\/a> can not be applied here as we use keys as index in counting sort. Here keys are floating point numbers.<br \/>\nThe idea is to use bucket sort. Following is bucket algorithm.<\/p>\n<pre><strong>bucketSort(arr[], n)<\/strong>\r\n1) Create n empty buckets (Or lists).\r\n2) Do following for every array element arr[i].\r\n.......a) Insert arr[i] into bucket[n*array[i]]\r\n3) Sort individual buckets using insertion sort.\r\n4) Concatenate all sorted buckets.\r\n<\/pre>\n[ad type=\u201dbanner\u201d]\n<p>Following diagram (taken from <a href=\"http:\/\/www.flipkart.com\/introduction-algorithms-3rd\/p\/itmdvd93bzvrnc7b?pid=9788120340077&affid=sandeepgfg\" target=\"_blank\" rel=\"noopener noreferrer\">CLRS book<\/a>) demonstrates working of bucket sort.<\/p>\n<p><a href=\"http:\/\/www.geeksforgeeks.org\/wp-content\/uploads\/BucketSort.png\"><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter size-full wp-image-128702\" src=\"http:\/\/www.geeksforgeeks.org\/wp-content\/uploads\/BucketSort.png\" alt=\"BucketSort\" width=\"320\" height=\"240\" \/><\/a><\/p>\n<p><strong>Time Complexity:<\/strong> If we assume that insertion in a bucket takes O(1) time then steps 1 and 2 of the above algorithm clearly take O(n) time. The O(1) is easily possible if we use a linked list to represent a bucket (In the following code, C++ vector is used for simplicity). Step 4 also takes O(n) time as there will be n items in all buckets.<br \/>\nThe main step to analyze is step 3. This step also takes O(n) time on average if all numbers are uniformly distributed (please refer <a href=\"http:\/\/www.flipkart.com\/introduction-algorithms-3rd\/p\/itmdvd93bzvrnc7b?pid=9788120340077&affid=sandeepgfg\" target=\"_blank\" rel=\"noopener noreferrer\">CLRS book<\/a> for more details)<\/p>\n<p>Following is C++ implementation of the above algorithm.<\/p>\n[pastacode lang=\u201dc\u201d manual=\u201d%2F%2F%20C%2B%2B%20program%20to%20sort%20an%20array%20using%20bucket%20sort%0A%23include%20%3Ciostream%3E%0A%23include%20%3Calgorithm%3E%0A%23include%20%3Cvector%3E%0Ausing%20namespace%20std%3B%0A%20%0A%2F%2F%20Function%20to%20sort%20arr%5B%5D%20of%20size%20n%20using%20bucket%20sort%0Avoid%20bucketSort(float%20arr%5B%5D%2C%20int%20n)%0A%7B%0A%20%20%20%20%2F%2F%201)%20Create%20n%20empty%20buckets%0A%20%20%20%20vector%3Cfloat%3E%20b%5Bn%5D%3B%0A%20%20%20%20%0A%20%20%20%20%2F%2F%202)%20Put%20array%20elements%20in%20different%20buckets%0A%20%20%20%20for%20(int%20i%3D0%3B%20i%3Cn%3B%20i%2B%2B)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20int%20bi%20%3D%20n*arr%5Bi%5D%3B%20%2F%2F%20Index%20in%20bucket%0A%20%20%20%20%20%20%20b%5Bbi%5D.push_back(arr%5Bi%5D)%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F%2F%203)%20Sort%20individual%20buckets%0A%20%20%20%20for%20(int%20i%3D0%3B%20i%3Cn%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20sort(b%5Bi%5D.begin()%2C%20b%5Bi%5D.end())%3B%0A%20%0A%20%20%20%20%2F%2F%204)%20Concatenate%20all%20buckets%20into%20arr%5B%5D%0A%20%20%20%20int%20index%20%3D%200%3B%0A%20%20%20%20for%20(int%20i%20%3D%200%3B%20i%20%3C%20n%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20for%20(int%20j%20%3D%200%3B%20j%20%3C%20b%5Bi%5D.size()%3B%20j%2B%2B)%0A%20%20%20%20%20%20%20%20%20%20arr%5Bindex%2B%2B%5D%20%3D%20b%5Bi%5D%5Bj%5D%3B%0A%7D%0A%20%0A%2F*%20Driver%20program%20to%20test%20above%20funtion%20*%2F%0Aint%20main()%0A%7B%0A%20%20%20%20float%20arr%5B%5D%20%3D%20%7B0.897%2C%200.565%2C%200.656%2C%200.1234%2C%200.665%2C%200.3434%7D%3B%0A%20%20%20%20int%20n%20%3D%20sizeof(arr)%2Fsizeof(arr%5B0%5D)%3B%0A%20%20%20%20bucketSort(arr%2C%20n)%3B%0A%20%0A%20%20%20%20cout%20%3C%3C%20%22Sorted%20array%20is%20%5Cn%22%3B%0A%20%20%20%20for%20(int%20i%3D0%3B%20i%3Cn%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20cout%20%3C%3C%20arr%5Bi%5D%20%3C%3C%20%22%20%22%3B%0A%20%20%20%20return%200%3B%0A%7D\u201d message=\u201d\u201d highlight=\u201dc\u201d provider=\u201dmanual\u201d\/]\n<p>\u00a0<\/p>\n<p><strong>Output:<\/strong><\/p>\n<pre>Sorted array is\r\n0.1234 0.3434 0.565 0.656 0.665 0.897<\/pre>\n[ad type=\u201dbanner\u201d]\n","protected":false},"excerpt":{"rendered":"<p>Bucket Sort &#8211; Searching and sorting &#8211; A simple way is to apply a comparison based sorting algorithm. The lower bound for Comparison based sorting algorithm (Merge Sort, Heap Sort, Quick-Sort .. etc) is \u03a9(n Log n), i.e., they cannot do better than nLogn.<\/p>\n","protected":false},"author":1,"featured_media":25196,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[83504,71670],"tags":[70054,71832,71840,71988,72003,70478,71991,71995,71997,70052,70263,70268,71987,70461,71121,70978,71879,71122,71129,71874,70780,71993,71999,72008,72000,72001,71833,72006,70497,71868,70048,71989,71996,70271,70928,71994,70017,71225,72009,70262,71262,71992,71267,70046,71145,70977,71266,71870,71846,72007,71120,70016,71697,71674,72004,71125,71695,70020,71882,70019,71998,71990,72002,72005],"class_list":["post-25194","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-bucket-sort","category-searching-and-sorting","tag-algorithm","tag-algorithm-of-bucket-sort","tag-algorithm-of-radix-sort","tag-algorithm-of-sorting","tag-average-bucket-size","tag-avl-tree","tag-basket-case","tag-bein-sport","tag-bin-bucket","tag-binary-search","tag-binary-search-tree","tag-binary-tree","tag-bitbucket","tag-bubble-sort","tag-bubble-sort-algorithm","tag-bubble-sort-c","tag-bubble-sort-concept","tag-bubble-sort-java","tag-bubble-sort-program","tag-bubble-sort-technique","tag-bubblesort","tag-bucket","tag-bucket-bin","tag-bucket-c","tag-bucket-order","tag-case-bucket","tag-comb-sort","tag-computer-sorting","tag-counting-sort","tag-counting-sort-algorithm","tag-data-structures","tag-different-sorting-algorithms-in-java","tag-excel-sort","tag-heap-sort","tag-heap-sort-algorithm","tag-heapsort-tutorial","tag-insertion-sort-algorithm","tag-insertion-sort-program","tag-memory-bucket","tag-merge-sort","tag-merge-sort-algorithm","tag-merge-sort-definition","tag-quick-sort-algorithm","tag-quicksort","tag-quicksort-c","tag-quicksort-java","tag-radix-sort-algorithm","tag-radix-sort-algorithm-in-data-structure","tag-radix-sort-applications","tag-range-bucket","tag-selection-sort","tag-selection-sort-algorithm","tag-shell-sort","tag-sort","tag-sort-2","tag-sort-code","tag-sorted","tag-sorting-algorithms","tag-sorting-algorithms-explained-with-examples","tag-sorting-algorithms-in-java","tag-sorting-bins","tag-sorting-techniques-in-java","tag-the-bubble-sort","tag-time-bucket"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25194","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=25194"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25194\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media\/25196"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=25194"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=25194"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=25194"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}