package basic_class_01;
/**
* @Author qgfzzzzzz
* @Date 19-3-1
* @Version 1.0
*
* This Algorithm is bucket sort
* time : o(n)
* space : o(n)
*/
public class Code_06_BucketSort {
public static void bucketSort(int[] arr){
if(arr == null || arr.length < 2){
return;
}
int max = Integer.MIN_VALUE;
for(int i = 0; i < arr.length; i++){
max = Math.max(max, arr[i]);
}
int[] bucket = new int[max + 1];
for(int k = 0; k < arr.length; k++){
bucket[arr[k]]++;
}
int i = 0;
for(int j = 0; j < bucket.length; j++){
while(bucket[j]-- > 0){
arr[i++] = j;
}
}
}
public static void main(String[] args){
int[] arr =
{2, 6, 8 , 6, 4, 9, 1, 2, 5, 6, 4, 4, 9, 3};
bucketSort(arr);
for (int t :
arr) {
System.out.print(t + " ");
}
}
}