publicstaticvoidunstableSort(int[] arr) { intmin= arr[0], max = arr[0]; for (inti=1; i < arr.length; i++) { if (arr[i] < min) min = arr[i]; if (arr[i] > max) max = arr[i]; }
int[] counts = newint[max - min + 1]; for (int e : arr){ counts[e - min]++; }
for (inti=0, j = 0; i < counts.length; i++) { while ((counts[i]--) > 0) { arr[j++] = i + min; } } }
publicstaticvoidstableSort(int[] arr) { intmin= arr[0], max = arr[0]; for (int e : arr) { if (e < min) min = e; if (e > max) max = e; }
int[] counts = newint[max - min + 1]; //counts[i]保存着arr中等于 i+min 的元素个数 for (int e : arr) { counts[e - min]++; }
//counts[i]保存着小于等于 i+min 的元素个数 for (inti=1; i < counts.length; i++) { counts[i] += counts[i - 1]; }