# Thread: bucket sort algorithm help

1. Member
Join Date
Sep 2016
Posts
32
Rep Power
0

## bucket sort algorithm help

I am using a bucket sort for a program that i have to write for class. I am stuck and cannot figure out how to go further. I am not sure what the E[] list argument is in the bucket sort method. If anyone could fill me in as to how I need to convert my integer array before it is sent to the method that would be great. Thanks!

Obviously this code is partial as I was taking some shots in the dark with array lists and whatnot.

Java Code:
```public class Test extends Application {

@Override
public void start(Stage primaryStage) {

int[] listOriginal = new int[50];

for (int i = 0; i < listOriginal.length; i++) {
int num = (int) (Math.random() * 100);
listOriginal[i] = num;
}

Integer[] list0 = new Integer[listOriginal.length];

for (int i = 0; i < listOriginal.length; i++) {
list0[i] = list5K[i];
}

List<Integer> intList = new ArrayList<Integer>();

intList.addAll(Arrays.asList(list0));

bucketSort(intList);
}

public void bucketSort(E[] list) {
E[] bucket = (E[]) new java.util.ArrayList[t + 1];

for (int i = 0; i < list.length; i++) {
int key = list[i].getKey();

if (bucket[key] == null) {
bucket[key] = new java.util.ArrayList<>();
}

bucket[key].add(list[i]);
}

int k = 0;
for (int i = 0; i < bucket.length; i++) {
if (bucket[i] != null) {
for (int j = 0; j < bucket[i].size(); j++) {
list[k++] = bucket[i].get(j);
}
}
}
}

public static void main(String[] args) {
launch(args);
}
}```

2. Moderator
Join Date
Apr 2009
Posts
13,541
Rep Power
27

## Re: bucket sort algorithm help

That is a generic method.
If you haven't done generics then I'm not sure where you plucked that from.

3. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## Re: bucket sort algorithm help

I presume you understand the algorithm. So I just took a look at bucket sort on Wiki (never done it myself).
Based on that, the first thing I would do would be to focus on populating the buckets. Once that is done,
display the results and verify it meets the requirements. Then you can focus on sorting each bucket with some
algorithm. Once again, print the results and verify. Then sort the buckets as individual elements and verify. Then just
concatenate the buckets together. The point is, break up your code into smaller steps. It makes programming much easier.

Regards,
Jim

4. Member
Join Date
Sep 2016
Posts
32
Rep Power
0

## Re: bucket sort algorithm help

We have gone over generics, but I can't quite figure out how send the function the correct argument.

5. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## Re: bucket sort algorithm help

I suggest you simply replace E[] with int[] since that is what you are sorting. You can't declare a generic type in an method argument unless the
generic type is part of the class definition or declared static.

EDIT. Actually, int[] won't work since you are using a List there. So put List<Integer> list as the argument. Just make certain the types
you choose as arguments are appropriate for the target method.

This is related to my previous post. Get the thing working step by step. Then, once it is working, you can make it more flexible by
employing generics. BTW, generics and arrays don't play well together. So get used to using some List implementation. You can always
used a list of lists. E.g. List<List<Integer>>.
Regards,
Jim
Last edited by jim829; 09-30-2016 at 07:54 PM.

6. Senior Member
Join Date
Sep 2014
Location
MA, USA
Posts
399
Rep Power
6

## Re: bucket sort algorithm help

the teacher might have given the OP the generic argument so the OP checks for the implementation of the Comparable<T> Interface.

7. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## Re: bucket sort algorithm help

Perhaps, but then the teacher could have just provided the following:

Java Code:
```public void bucketSort(Comparable<?>[] list) {
...```
Regards,
Jim

8. Senior Member
Join Date
Sep 2014
Location
MA, USA
Posts
399
Rep Power
6

## Re: bucket sort algorithm help

Correct, but that would have been a little too obvious.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•