Results 1 to 8 of 8
  1. #1
    hornet7288 is offline Member
    Join Date
    Sep 2016
    Posts
    32
    Rep Power
    0

    Default 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. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default 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.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

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

    Default 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
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    hornet7288 is offline Member
    Join Date
    Sep 2016
    Posts
    32
    Rep Power
    0

    Default 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. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default 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.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

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

    Default 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. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default 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
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

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

    Default Re: bucket sort algorithm help

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

Similar Threads

  1. Replies: 0
    Last Post: 10-03-2011, 12:27 PM
  2. bucket sort, list in java
    By someone in forum New To Java
    Replies: 10
    Last Post: 04-11-2011, 11:13 PM
  3. problem with my bucket sort
    By someone in forum New To Java
    Replies: 8
    Last Post: 04-11-2011, 11:09 PM
  4. How to sort a list using Bubble sort algorithm
    By Java Tip in forum Algorithms
    Replies: 3
    Last Post: 04-29-2008, 08:04 PM
  5. Help with sort algorithm
    By zoe in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 06:09 AM

Posting Permissions

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