Questions tagged [bucket-sort]

Bucket sort is a generic variant of the pigeonhole sorting algorithm which works by splitting a list into "buckets" based on arbitrary boundaries, sorting the buckets, and recombining the buckets in order.

A Bucket Sort takes arbitrary boundaries, then iterates through the input array and places the elements into a new array (its "bucket") based on the boundaries. After this, the sub-arrays are sorted separately. Finally, the sub-arrays are merged in order. Note that the Bucket Sort is not capable of sorting an array on its own; it must either recursively call itself, and recognize when passed an array of length 0 or 1, or call a different algorithm, in order to sort the buckets.

Return to

98 questions
59
votes
7 answers

Radix sort vs Counting sort vs Bucket sort. What's the difference?

I am reading the definitions of radix, counting and bucket sorts and it seems that all of them are just the code below: public static void sort(int[] a, int maxVal){ int [] bucket=new int[maxVal+1]; for (int i=0; i
good_evening
  • 19,773
  • 60
  • 178
  • 288
10
votes
2 answers

Can the Duplicate Characters in a string be Identified and Quantified in O(n)?

This comment suggests that there is a O(n) alternative to my O(n log n) solution to this problem: Given string str("helloWorld") the expected output is: l = 3 o = 2 My solution was to do this: sort(begin(str), end(str)); for(auto start =…
Jonathan Mee
  • 35,107
  • 16
  • 95
  • 241
8
votes
5 answers

What is the worst case complexity for bucket sort?

I just read the Wikipedia page about Bucket sort. In this article they say that the worst case complexity is O(n²). But I thought the worst case complexity was O(n + k) where k are the number of buckets. This is how I calculate this complexity: Add…
nist
  • 1,628
  • 3
  • 16
  • 23
8
votes
1 answer

When should I choose bucket sort over other sorting algorithms?

When is bucket sort algorithm the best method to use for sorting? Is there a recommended guide in using them based on the size, type of data structure?
Rony
  • 193
  • 1
  • 2
  • 7
8
votes
1 answer

Sort a series of n numbers between [0,2k], Where between each pair exists: |Ai-Aj|>=k/n

Let A1,A2,...,An be real numbers between [0,2k] (k is constant). It's known that for any pair of Ai,AJ then |Ai-Aj|>=k/n, Describe an algorithm sorting the numbers in O(n) runtime worst-case. I know that the answer should be bucket-sort. Can't…
StationaryTraveller
  • 1,261
  • 1
  • 17
  • 27
8
votes
3 answers

Efficient bucket-sort on GPU

For a current OpenCL GPGPU project, I need to sort elements in an array according to some key with 64 possible values. I need the final array to have all elemens with the same key to be contiguous. It's sufficient to have an associative array…
leemes
  • 42,229
  • 18
  • 115
  • 172
6
votes
1 answer

Under what conditions do these non-comparison sorts run in linear time?

I am exploring the following algorithms: Counting Sort Radix Sort Bucket Sort I know all three are capable of running in linear time at best case, but I am having trouble with understanding when those cases occur, except in the case of counting…
ZAX
  • 918
  • 3
  • 19
  • 48
5
votes
6 answers

Bucketing in R or SQL

I am completely stumped on a problem and would like some guidance. I am picking random sets of 8 numbers from the set of 1 to 8 (for example, 5,6,8,1,3,4,2,7) and trying to bucket those numbers as subsets of sequential numbers according to the…
5
votes
1 answer

Map.toList performance in Haskell

In the code below I'm benchmarking my implementation of Bucket Sort. The bucketsort function uses the result from _bucketsort but flattens it to a single list. To my surprise this process (Map.toList) takes a lot of time. module Main where import…
Htbaa
  • 2,319
  • 18
  • 26
4
votes
1 answer

american flag sort optimization

I am trying to implement American Bucket Sort. Wiki says "first to count the number of objects that will fall in each bin, and second to place each object in its bucket." In second phase, when placing objects in proper buckets, Do I need to use…
bfaskiplar
  • 795
  • 6
  • 20
4
votes
1 answer

what would be a good hash function for bucket sort?

First, most places that claim to have an implementation of bucket sort are actually implementing counting sort. My question is about bucket sort as implemented on Geek Viewpoint and Wikipedia. I don't really get/like the hash function on Geek…
Katedral Pillon
  • 14,266
  • 20
  • 87
  • 182
4
votes
3 answers

Mapping fields of weakly related tables in SQL

I'm searching for an SQL-Query that can map a set of items of an individual size to a set off buckets of individual size. I would like to satisfy the following conditions: The size of a bucket has to be bigger or equal the size of an item. Every…
user3112922
4
votes
3 answers

How Bucket sort is considered under Linear Sorting?

I would like to explore my analysis regarding Bucket sort as below. There are many ways that Bucket sort can be implemented. Some of them are as follows. Type…
MrA
  • 509
  • 3
  • 7
  • 16
4
votes
1 answer

How many comparisons are needed in worst case if we have to sort 7 numbers each of 4 digit?

How many comparisons are needed in worst case if we have to sort 7 numbers each of 4 digit ?(Radix sort) Options are- 40,38,47,280 . My solution-I have taken 10 buckets( 0 to 9)(linked list) . Then for every number for ith digit I have put it into…
Manish
  • 744
  • 1
  • 9
  • 18
3
votes
2 answers

c# implementing a bucket sort algorithm

Good evening everyone here! I created a bucket sort algorithm, but it throws me an error that index is out of range. Could you please tell me where is the problem? I can't find the solution by myself, that's why I'm asking for your help public int[]…
finsters
  • 149
  • 1
  • 9
1
2 3 4 5 6 7