https://leetcode.com/problems/maximum-gap/
用java实现了一下,采用LSD(最低位优先法):
(未考虑有负数的情况)
private static void radixSort(int[] nums) {
int max = nums[0];
for (int i = 1; i < nums.length; i++) {
max = Math.max(max, nums[i]);
}
for(int divisor=1;max>=divisor;divisor*=10) {
List<Integer>[] buckets = new List[10];
for(int i=0;i<=9;++i)
buckets[i] = new ArrayList<Integer>();
for(int num : nums) {
int dd = num/divisor;
int b = dd%10;
buckets[b].add(num);
}
int i = 0;
for(int b=0;b<=9;++b) {
for(int num :buckets[b]) {
nums[i++] = num;
}
}
}
}