为什么选基数排序
基数排序在合适的时候,由于其不基于比较,能够实现O(N)的排序时间复杂度,会比快排还快。
下面的测试就告诉了我们这个问题,合适的时候确实比sort()快很多。
基数排序的编程实现
import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;
import java.util.Random;
public class RadixSort {
/*
* Radix sort an array of Strings
* Assume all are all ASCII
* Assume all have same length
*/
@SuppressWarnings("unchecked")
public static void radixSortA(String [] arr, int stringLen) {
final int BUCKETS = 256;
ArrayList<String>[] buckets = new ArrayList[BUCKETS];
for(int i = 0; i < BUCKETS; i++) {
buckets[i] = new ArrayList<>();
}
for(int pos = stringLen - 1; pos >= 0; pos--) {
for(String s : arr) {
buckets[s.charAt(pos)].add(s);
}
int index = 0;
for(ArrayList<String> thisBucket : buckets) {
for(String s : thisBucket) {
arr[index++] = s;
}
thisBucket.clear();
}
}
}
/*
* Counting radix sort an array of Strings
* Assume all are all ASCII
* Assume all have same length
*/
public static void countingRadixSort(String [] arr, int stringLen) {
final int BUCKETS = 256;
int N = arr.length;
String [] buffer = new String[N];
String [] in = arr;
String [] out = buffer;
for(int pos = stringLen-1; pos >= 0; pos--) {
int[] count = new int [BUCKETS+1];
for(int i = 0; i < N; i++) {
count[in[i].charAt(pos)+1]++;
}
for(int b = 1; b <= BUCKETS; b++) {
count[b] += count[b-1];
}
for(int i = 0; i < N; i++) {
out[count[in[i].charAt(pos)]++] = in[i];
}
// swap in and out roles
String [] tmp = in;
in = out;
out = tmp;
}
// if odd number of passes, in is buffer, out is arr; so copy back
if(stringLen % 2 == 1) {
for(int i = 0; i < arr.length; i++) {
out[i] = in[i];
}
}
}
/*
* Radix sort an array of Strings
* Assume all are all ASCII
* Assume all have length bounded by maxLen
*/
@SuppressWarnings("unchecked")
public static void radixSort(String [] arr, int maxLen) {
final int BUCKETS = 256;
ArrayList<String> [] wordsByLength = new ArrayList[maxLen+1];
ArrayList<String> [] buckets = new ArrayList[BUCKETS];
for(int i = 0; i < wordsByLength.length; i++) {
wordsByLength[i] = new ArrayList<>();
}
for(int i = 0; i < BUCKETS; i++) {
buckets[i] = new ArrayList<>();
}
for(String s : arr) {
wordsByLength[s.length()].add(s);
}
int index = 0;
for(ArrayList<String> wordList : wordsByLength) {
for(String s : wordList) {
arr[index++] = s;
}
}
int startingIndex = arr.length;
for(int pos = maxLen - 1; pos >= 0; pos--) {
startingIndex -= wordsByLength[pos+1].size();
for(int i = startingIndex; i < arr.length; i++) {
buckets[arr[i].charAt(pos)].add(arr[i]);
}
index = startingIndex;
for(ArrayList<String> thisBucket : buckets) {
for(String s : thisBucket) {
arr[index++] = s;
}
thisBucket.clear();
}
}
}
}
测试
public class RadixSortTest {
public static void main(String [] args) {
List<String> lst = new ArrayList<>();
Random r = new Random();
final int LEN = 5;
for(int i = 0; i < 100000; i++) {
String str = "";
int len = LEN; // 3 + r.nextInt( 7 ); // between 3 and 9 characters
for(int j = 0; j < len; j++) {
str += (char) ('a' + r.nextInt(26));
}
lst.add(str);
}
String [] arr1 = new String[lst.size()];
String [] arr2 = new String[lst.size()];
lst.toArray(arr1);
lst.toArray(arr2);
long start, end;
start = System.currentTimeMillis();
Arrays.sort(arr1);
end = System.currentTimeMillis();
System.out.println("Elapsed: " + (end-start));
start = System.currentTimeMillis();
countingRadixSort(arr2, LEN);
end = System.currentTimeMillis();
System.out.println("Elapsed: " + (end - start));
for(int i = 0; i < arr1.length; i++) {
if(!arr1[i].equals(arr2[i])) {
System.out.println("OOPS!!");
}
}
}
}
测试结果:
Elapsed: 324
Elapsed: 175