package com.sort;

import java.util.ArrayList;
import java.util.List;

public class BucketSort {
public static void bucketSort(double [] data){
List<Double>[] b=new ArrayList[data.length];
for(int i=0;i<b.length;i++){
b[i]=new ArrayList<Double>();
}
for(int i=0;i<data.length;i++){
b[(int)Math.floor(data.length*data[i])].add(data[i]);
}
for(int i=0;i<data.length;i++){
insertSort(b[i]);
}
int k=0;
for(int i=0;i<b.length;){
for(int j=0;j<b[k].size();j++){
data[i]=b[k].get(j);
i++;
}
k++;
}
}
private static void insertSort(List<Double> source){
for(int i=0;i<source.size();i++){
for(int j=0;j<i;j++){
if(source.get(i)<source.get(j)){
double tmp=source.get(j);
source.set(j, source.get(i));
if(i-j==1){
source.set(i, tmp);
}else{
for(int k=i;k>=j+2;k--){
source.set(k, source.get(k-1));
}
source.set(j+1, tmp);
}

}
}
}
}
public static void main(String[] args) {
double [] data=new double[]{0.1,0.3,0.2,0.88,0.87,0.33,0.37,0.35};
for(int i=0;i<data.length;i++){
System.out.print(data[i]+" ");
}
System.out.println();
bucketSort(data);
for(int i=0;i<data.length;i++){
System.out.print(data[i]+" ");
}
System.out.println();
}

}