1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
| /**
* 线性排序算法综合测试
*/
public class LinearSortingTest {
public static void main(String[] args) {
System.out.println("=== 线性排序算法综合测试 ===");
testCountingSort();
testBucketSort();
testRadixSort();
testSpecialApplications();
LinearSortingAnalyzer.benchmarkWithDifferentDistributions();
}
private static void testCountingSort() {
System.out.println("\n1. 计数排序测试:");
// 基础测试
int[] basicData = {4, 2, 2, 8, 3, 3, 1};
System.out.println("基础计数排序:");
CountingSort.countingSortBasic(basicData);
// 稳定性测试
int[] stableData = {4, 2, 2, 8, 3, 3, 1};
System.out.println("\n稳定版计数排序:");
int[] result = CountingSort.countingSortStable(stableData);
System.out.println("结果: " + Arrays.toString(result));
// 学生成绩排序
CountingSort.Student[] students = {
new CountingSort.Student(1, "Alice", 85),
new CountingSort.Student(2, "Bob", 92),
new CountingSort.Student(3, "Charlie", 85),
new CountingSort.Student(4, "Diana", 78)
};
System.out.println("\n学生成绩排序:");
CountingSort.Student[] sortedStudents = CountingSort.countingSortStudents(students);
for (CountingSort.Student student : sortedStudents) {
System.out.println(student);
}
}
private static void testBucketSort() {
System.out.println("\n2. 桶排序测试:");
// 浮点数桶排序
double[] floatData = {0.897, 0.565, 0.656, 0.1234, 0.665, 0.3434};
System.out.println("浮点数桶排序:");
BucketSort.bucketSortBasic(floatData, 3);
// 整数桶排序
int[] intData = {29, 25, 3, 49, 9, 37, 21, 43};
System.out.println("\n整数桶排序:");
int[] result = BucketSort.bucketSortGeneral(intData, 4);
System.out.println("结果: " + Arrays.toString(result));
// 成绩分析
int[] grades = {85, 92, 78, 96, 85, 89, 76, 91, 88, 94};
System.out.println("\n成绩分析:");
BucketSort.GradeDistribution.analyzeGrades(grades);
}
private static void testRadixSort() {
System.out.println("\n3. 基数排序测试:");
// LSD基数排序
int[] lsdData = {170, 45, 75, 90, 2, 802, 24, 66};
System.out.println("LSD基数排序:");
int[] lsdResult = RadixSort.radixSortLSD(lsdData);
System.out.println("最终结果: " + Arrays.toString(lsdResult));
// 字符串排序
String[] strings = {"abc", "def", "aba", "xyz", "aaa"};
System.out.println("\n字符串基数排序:");
String[] strResult = RadixSort.radixSortStrings(strings, 3);
System.out.println("结果: " + Arrays.toString(strResult));
// 负数排序
int[] negativeData = {-5, 3, -2, 8, -1, 0, 7};
System.out.println("\n包含负数的基数排序:");
int[] negResult = RadixSort.radixSortWithNegatives(negativeData);
System.out.println("结果: " + Arrays.toString(negResult));
}
private static void testSpecialApplications() {
System.out.println("\n4. 特殊应用测试:");
// IP地址排序
String[] ips = {
"192.168.1.1",
"10.0.0.1",
"192.168.1.100",
"172.16.0.1",
"192.168.0.1"
};
System.out.println("IP地址排序:");
RadixSort.IPAddressSorter.sortIPAddresses(ips);
// 算法选择建议
System.out.println("\n算法选择建议:");
LinearSortingAnalyzer.AlgorithmSuitability.analyzeDataSuitability();
}
}
|