简体   繁体   中英

bucket size in bucket sort

I am trying to write code for bucket sort, but am confused in bucket size of each bucket. My code is below. input array: {12, 11, 13, 5, 6, 7,10,22,4,16,1,26};

I am passing bucket size of each bucket >3 then I dont get the output in sorted order. It gives perfect ans for bucket size 1 and 2

public void bucsort(int[] arr,int bucketSize){

        if(arr.length==0) return;

        int max=arr[0];
        int min=arr[0];

        for(int i=0; i<arr.length;i++){
            if(arr[i]<min)
            {
                min=arr[i];
            }
            else
                max=arr[i];
        }
        int bucketCount= (max - min) / bucketSize + 1;
         List<List<Integer>> buckets = new ArrayList<List<Integer>>(bucketCount);
        // int divider= (max+1)/bucketCount;

         for (int i = 0; i < bucketCount; i++) {
                buckets.add(new ArrayList<Integer>());
            }
         for (int i = 0; i < arr.length; i++) {

                buckets.get((arr[i]-min) / bucketSize).add(arr[i]);
            }
         int currentIndex = 0;
            for (int i = 0; i < buckets.size(); i++) {
                Integer[] bucketArray = new Integer[buckets.get(i).size()];
                bucketArray = buckets.get(i).toArray(bucketArray);
                InsertionSort(bucketArray);
                for (int j = 0; j < bucketArray.length; j++) {
                    arr[currentIndex++] = bucketArray[j];
                }
            }       
    }

Is there any relation between no. of buckets and its size ?

I edited my method for max-min function and also debugged the program. There seems to be some mistake in my insertion sort

the code is:

public void InsertionSort(Integer[] arr){


        for(int i=1; i<arr.length; i++){
            int value=arr[i];
            int hole=i;

            while(hole>0 && arr[hole-1]>value){

                arr[hole]=arr[hole-1];

                hole--;
            }

            arr[hole-1]=value;
        }
    }

main func

public static void main(String[] args) {

        int arr[] = {12, 11, 13, 5, 6, 7,10,22,4,16,1,26};

        BucketSort ob = new BucketSort();
        ob.bucsort(arr, 5);
        printArray(arr);
    }
    static void printArray(int arr[])
    {
        int n = arr.length;
        for (int i=0; i<n; ++i)
            System.out.print(arr[i] + " ");
        System.out.println();
    }

My output for bucket size 5 : 5 1 4 6 7 10 12 11 13 16 22 26 for size 3: 1 5 4 6 7 12 10 11 13 16 22 26 for size 2: 1 4 5 6 7 10 12 11 13 16 22 26

Finding max-min is wrong...(you have some logical error)

int minValue = array[0];
        int maxValue = array[0];
        for (int i = 1; i < array.Length; i++) {
            if (array[i] < minValue) {
                minValue = array[i];
            } else if (array[i] > maxValue) {
                maxValue = array[i];
            }
        }

On your code:

     1 4 3 
min  1 1 1
max  1 4 3

This will be the correct implemenation

for (i = 1; i < length; i++) {
            j = i;
            while (j > 0 && arr[j - 1] > arr[j]) {
                  tmp = arr[j];
                  arr[j] = arr[j - 1];
                  arr[j - 1] = tmp;
                  j--;
            }
}

I will debug your code when I get time..

In your InsertionSort method you do

        int value=arr[i];
        int hole=i;

        while(hole>0 && arr[hole]>value){

At this point arr[hole] will always equal value , so you never get into the while loop. So nothing gets sorted. For small bucket sizes, you may be lucky that it doesn't matter. For bucket size 1, it certainly doesn't. Even for bucket size 2 you have a single error in the sorting. I get:

1 4 5 6 7 10 12 11 13 16 22 26

12 comes before 11 because those two numbers end up in the same bucket and don't get sorted.

I took this from your comment: while (hole > 0 && arr[hole - 1] > value) { . On your request, now the method looks like this:

public void insertionSort(Integer[] arr) { // renamed to small i since it’s a method

    for (int i = 1; i < arr.length; i++) {
        int value = arr[i];
        int hole = i;

        while (hole > 0 && arr[hole - 1] > value) {

            arr[hole] = arr[hole - 1];

            hole--;
        }

        arr[hole] = value;
    }
}

Now I get correct sorting with all bucket sizes from 1 to 19. If you still have a problem, there must be something we are not doing the same way.

It's an aside, but as has been mentioned a couple of times, there is a bug in your code for finding max . I tried this input:

    int arr[] = { 3, 10, 4 };

I get a max of 4 (expected: 10) and then a java.lang.IndexOutOfBoundsException: Index: 1, Size: 1 at this line:

            buckets.get((arr[i]-min) / bucketSize).add(arr[i]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM