基数排序
算法描述:
分别比较个位,十位,百位,千位等数据的大小,比较方法依赖其他排序方法,可以计数排序。时间复杂度:O(d(r+n))
空间复杂度:O(rd+n)
注:r代表关键字基数,d代表长度,n代表关键字个数
java实现:
public static void radixSort(int[] arr, int radix, int d) {
int[] tmp = new int[arr.length]; // 缓存数组
int[] buckets = new int[radix]; // buckets用于记录待排序元素的信息,定义了radix个桶
for (int i = 0, rate = 1; i < d; i++) {
Arrays.fill(buckets, 0); // 重置count数组,开始统计下一个关键字
tmp = Arrays.copyOf(arr, arr.length);
// 以下是计数排序的实现
for (int j = 0; j < arr.length; j++) {
int subKey = (tmp[j] / rate) % radix;
buckets[subKey]++;
}
for (int j = 1; j < radix; j++) {
buckets[j] = buckets[j] + buckets[j - 1];
}
for (int m = arr.length - 1; m >= 0; m--) {
int subKey = (tmp[m] / rate) % radix; // 计算桶的位置
int position = buckets[subKey];
arr[position - 1] = tmp[m];
buckets[subKey] -= 1;
}
rate *= radix; // 从低位到高位
}
}
GitHub地址:
https://github.com/xckNull/Algorithms-introduction.git