Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

bucket sort java

public class Main 
{
   public static int[] bucket_sort(int[] arr, int max_value) 
    {
        int[] bucket = new int[max_value + 1];
        int[] sorted_arr = new int[arr.length];
 
        for (int i= 0; i <arr.length; i++)
            bucket[arr[i]]++;
 
        int pos = 0;
        for (int i = 0; i < bucket.length; i++)
            for (int j = 0; j < bucket[i]; j++)
                sorted_arr[pos++] = i;
 
        return sorted_arr;
    }
 
 
    static int maxValue(int[] arr) 
    {
        int max_value = 0;
        for (int i = 0; i < arr.length; i++)
            if (arr[i] > max_value)
                max_value = arr[i];
        return max_value;
    }
 
    public static void main(String args[]) 
    {
        int[] arr ={80, 50, 30, 10, 90, 60, 0, 70, 40, 20, 50};
        int max_value = maxValue(arr);
 
        System.out.print("
Original : ");
        System.out.println(Arrays.toString(arr));
 
        System.out.print("
Sorted : ");
        System.out.println(Arrays.toString(bucket_sort(arr,max_value)));

    }
}
Comment

PREVIOUS NEXT
Code Example
Java :: java remove non numeric characters from string 
Java :: java betrag 
Java :: cast long to string java 
Java :: how to remove all whitespace from string java 
Java :: how to set the text of a jlabel to bold 
Java :: android hide keyboard 
Java :: get tfidf score for a sentence 
Java :: How can I put a parameter into a localized string in Android? 
Java :: java lowercase in a scanner 
Java :: how to put icons inside of an edittext android 
Java :: android hide soft keyboard 
Java :: stream distinct by property 
Java :: add label gui jframe 
Java :: collision java 
Java :: javafx listview get item index 
Java :: how to make an invisiblke button in swing 
Java :: array slice java 
Java :: fullscreen jframe 
Java :: java remove first element from array 
Java :: how to remove java from ubuntu 
Java :: clsoe keyboard android studio 
Java :: running time of java program 
Java :: convert string to char array in java 
Java :: How to find a target element within a search pool using binary search? 
Java :: java load file from resources 
Java :: file to multipartfile in java 
Java :: android volley 
Java :: java remove from arraylist while iterating 
Java :: create file android java specific folder 
Java :: how to delete character certain index in java 
ADD CONTENT
Topic
Content
Source link
Name
1+9 =