1 基数排序
1.1 原理
适用场景:手机号、英文单词等字符串。
1.2 实现
import java.util.Arrays;
/**
* 基数排序
*
* @author wgm
* @since 2021/4/18
*/
public class RadixSort {
public static void main(String[] args) {
String[] strings = {"18914021920", "13223132981", "13566632981", "13660891039", "13361323035",
"banana", "apple", "orange", "peach", "cherry", "qd", "", "abc", "qwe", "hhh", "a", "", "cws", "ope"};
System.out.println("排序前:" + Arrays.toString(strings));
System.out.println("排序后:" + Arrays.toString(radixSort(strings)));
}
private static String[] radixSort(String[] strings) {
if (strings == null || strings.length <= 1) {
return strings;
}
// 循环进行计数排序
final int radix = 128; // ascii,控制符串的ascii是0
int maxLength = getMaxLength(strings);
for (int i = maxLength - 1; i >= 0; i--) {
// 初始化桶
int[] countList = new int[radix];
// 计数排序
for (int j = 0; j < strings.length; j++) {
int index = getIndex(strings[j], i);
countList[index]++;
}
int sum = 0;
for (int j = 0; j < countList.length; j++) {
countList[j] += sum;
sum = countList[j];
}
String[] result = new String[strings.length];
for (int j = strings.length - 1; j >= 0; j--) {
int index = getIndex(strings[j], i);
int rank = countList[index]--;
result[rank - 1] = strings[j];
}
strings = result;
}
return strings;
}
private static int getIndex(String string, int i) {
if (i <= string.length() - 1) {
return string.charAt(i);
} else {
return 0;
}
}
private static int getMaxLength(String[] strings) {
int maxLength = 0;
for (int i = 0; i < strings.length; i++) {
if (strings[i].length() > maxLength) {
maxLength = strings[i].length();
}
}
return maxLength;
}
}
1.3 测试
D:\program\Java\jdk1.8.0_241\bin\java.exe -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:59356,suspend=y,server=n "-javaagent:C:\Users\GMWANG~1\AppData\Local\Temp\captureAgent925jars\debugger-agent.jar" -Dfile.encoding=UTF-8 -classpath "D:\program\Java\jdk1.8.0_241\jre\lib\charsets.jar;D:\program\Java\jdk1.8.0_241\jre\lib\deploy.jar;D:\program\Java\jdk1.8.0_241\jre\lib\ext\access-bridge-64.jar;D:\program\Java\jdk1.8.0_241\jre\lib\ext\cldrdata.jar;D:\program\Java\jdk1.8.0_241\jre\lib\ext\dnsns.jar;D:\program\Java\jdk1.8.0_241\jre\lib\ext\jaccess.jar;D:\program\Java\jdk1.8.0_241\jre\lib\ext\jfxrt.jar;D:\program\Java\jdk1.8.0_241\jre\lib\ext\localedata.jar;D:\program\Java\jdk1.8.0_241\jre\lib\ext\nashorn.jar;D:\program\Java\jdk1.8.0_241\jre\lib\ext\sunec.jar;D:\program\Java\jdk1.8.0_241\jre\lib\ext\sunjce_provider.jar;D:\program\Java\jdk1.8.0_241\jre\lib\ext\sunmscapi.jar;D:\program\Java\jdk1.8.0_241\jre\lib\ext\sunpkcs11.jar;D:\program\Java\jdk1.8.0_241\jre\lib\ext\zipfs.jar;D:\program\Java\jdk1.8.0_241\jre\lib\javaws.jar;D:\program\Java\jdk1.8.0_241\jre\lib\jce.jar;D:\program\Java\jdk1.8.0_241\jre\lib\jfr.jar;D:\program\Java\jdk1.8.0_241\jre\lib\jfxswt.jar;D:\program\Java\jdk1.8.0_241\jre\lib\jsse.jar;D:\program\Java\jdk1.8.0_241\jre\lib\management-agent.jar;D:\program\Java\jdk1.8.0_241\jre\lib\plugin.jar;D:\program\Java\jdk1.8.0_241\jre\lib\resources.jar;D:\program\Java\jdk1.8.0_241\jre\lib\rt.jar;D:\project\untitled\out\production\untitled;D:\program\JetBrains\IntelliJ IDEA 2020.1\lib\idea_rt.jar" RadixSort
Connected to the target VM, address: '127.0.0.1:59356', transport: 'socket'
排序前:[18914021920, 13223132981, 13566632981, 13660891039, 13361323035, banana, apple, orange, peach, cherry, qd, , abc, qwe, hhh, a, , cws, ope]
排序后:[, , 13223132981, 13361323035, 13566632981, 13660891039, 18914021920, a, abc, apple, banana, cherry, cws, hhh, ope, orange, peach, qd, qwe]
Disconnected from the target VM, address: '127.0.0.1:59356', transport: 'socket'
Process finished with exit code 0