fork(1) download
  1. class BucketSort {
  2.  
  3. public int[] bucketSort(int[] array) {
  4. int[] bucket = new int[100]; // create an array the size of the largest value + 1
  5.  
  6. // for every value in array, increment its corresponding bucket
  7. for (int i = 0; i < array.length; i++)
  8. bucket[array[i]]++;
  9.  
  10. int outPos = 0;
  11. for (int i = 0; i < bucket.length; i++) {
  12. if (bucket[i] > 0)
  13. array[outPos++] = i;
  14. }
  15.  
  16. return array;
  17. }
  18.  
  19. public static void main(String args[]){
  20. BucketSort bSort = new BucketSort();
  21. int[] array = { 77, 99, 44, 55, 22, 88, 11, 0, 66, 33 };
  22. int[] sortedArray = bSort.bucketSort(array);
  23. for (int i = 0; i < array.length; i++)
  24. System.out.println(sortedArray[i]);
  25.  
  26. }
  27. }
Success #stdin #stdout 0.07s 380224KB
stdin
Standard input is empty
stdout
0
11
22
33
44
55
66
77
88
99