public static void bucketSort(int[] arr) { int max = 0; int min = Integer.MAX_VALUE; for (int i = 0; i < arr.length; i++) { if (min > arr[i]) { min = arr[i]; } if (max < arr[i]) { max = arr[i]; } }
int[] results = new int[max - min + 1]; for (int i = 0; i < arr.length; i++) { results[adjustIndex(min, arr[i])]++; }
int i = 0, index = 0; while (i < results.length) { if (results[i] != 0) { arr[index++] = min + i; results[i]--; } else { i++; } } }
private static int adjustIndex(int min, int value) { return value - min; }