1.计数排序(Counting Sort)
1.1简介
1.2计数排序--最简单实现
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
protected void sort0() { // 找出最大值 int max = array[0]; for (int i = 1; i < array.length; i++) { if (array[i] > max) { max = array[i]; } } // O(n) // 开辟内存空间,存储每个整数出现的次数 int[] counts = new int[1 + max]; // 统计每个整数出现的次数 for (int i = 0; i < array.length; i++) { counts[array[i]]++; } // O(n) // 根据整数的出现次数,对整数进行排序 int index = 0; for (int i = 0; i < counts.length; i++) { while (counts[i]-- > 0) { array[index++] = i; } } // O(n) }
1.3计数排序--改进思路
1.4计数排序--改进实现
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
@Override protected void sort() { // 找出最值 int max = array[0]; int min = array[0]; for (int i = 1; i < array.length; i++) { if (array[i] > max) { max = array[i]; } if (array[i] < min) { min = array[i]; } } // 开辟内存空间,存储次数 int[] counts = new int[max - min + 1]; // 统计每个整数出现的次数 for (int i = 0; i < array.length; i++) { counts[array[i] - min]++; } // 累加次数 for (int i = 1; i < counts.length; i++) { counts[i] += counts[i - 1]; } // 从后往前遍历元素,将它放到有序数组中的合适位置 int[] newArray = new int[array.length]; for (int i = array.length - 1; i >= 0; i--) { newArray[--counts[array[i] - min]] = array[i]; } // 将有序数组赋值到array for (int i = 0; i < newArray.length; i++) { array[i] = newArray[i]; } }
1.5计数排序--自定义对象
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public static void main(String[] args) { Person[] persons = new Person[] { new Person(20, "A"), new Person(-13, "B"), new Person(17, "C"), new Person(12, "D"), new Person(-13, "E"), new Person(20, "F") }; // 找出最值 int max = persons[0].age; int min = persons[0].age; for (int i = 1; i < persons.length; i++) { if (persons[i].age > max) { max = persons[i].age; } if (persons[i].age < min) { min = persons[i].age; } } // 开辟内存空间,存储次数 int[] counts = new int[max - min + 1]; // 统计每个整数出现的次数 for (int i = 0; i < persons.length; i++) { counts[persons[i].age - min]++; } // 累加次数 for (int i = 1; i < counts.length; i++) { counts[i] += counts[i - 1]; } // 从后往前遍历元素,将它放到有序数组中的合适位置 Person[] newArray = new Person[persons.length]; for (int i = persons.length - 1; i >= 0; i--) { newArray[--counts[persons[i].age - min]] = persons[i]; } // 将有序数组赋值到array for (int i = 0; i < newArray.length; i++) { persons[i] = newArray[i]; } for (int i = 0; i < persons.length; i++) { System.out.println(persons[i]); } } private static class Person { int age; String name; Person(int age, String name) { this.age = age; this.name = name; } @Override public String toString() { return "Person [age=" + age + ", name=" + name + "]"; } }
2.基数排序(Radix Sort)
2.1简介
2.2基数排序--实现
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
@Override protected void sort() { // 找出最大值 int max = array[0]; for (int i = 1; i < array.length; i++) { if (array[i] > max) { max = array[i]; } } // 个位数: array[i] / 1 % 10 = 3 // 十位数:array[i] / 10 % 10 = 9 // 百位数:array[i] / 100 % 10 = 5 // 千位数:array[i] / 1000 % 10 = ... for (int divider = 1; divider <= max; divider *= 10) { countingSort(divider); } } protected void countingSort(int divider) { // 开辟内存空间,存储次数 int[] counts = new int[10]; // 统计每个整数出现的次数 for (int i = 0; i < array.length; i++) { counts[array[i] / divider % 10]++; } // 累加次数 for (int i = 1; i < counts.length; i++) { counts[i] += counts[i - 1]; } // 从后往前遍历元素,将它放到有序数组中的合适位置 int[] newArray = new int[array.length]; for (int i = array.length - 1; i >= 0; i--) { newArray[--counts[array[i] / divider % 10]] = array[i]; } // 将有序数组赋值到array for (int i = 0; i < newArray.length; i++) { array[i] = newArray[i]; } }
2.3基数排序--另一种思路