-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathSortMain.java
More file actions
109 lines (92 loc) · 3.29 KB
/
SortMain.java
File metadata and controls
109 lines (92 loc) · 3.29 KB
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
104
105
106
107
108
109
package leetcode8.sort;
import leetcode8.sort.Heap.HeapSort1;
import leetcode8.sort.bubble.BubbleSort1;
import leetcode8.sort.bubble.BubbleSort2;
import leetcode8.sort.bubble.BubbleSort3;
import leetcode8.sort.insert.InsertSort1;
import leetcode8.sort.insert.InsertSort2;
import leetcode8.sort.merge.MergeSort0;
import leetcode8.sort.merge.MergeSort1;
import leetcode8.sort.quick.QuickSort1;
import leetcode8.sort.quick.QuickSort2;
import leetcode8.sort.select.SelectSort1;
import leetcode8.sort.shell.ShellSort1;
import leetcode8.sort.shell.ShellSort2;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
public class SortMain {
private static int[][] demoArr = new int[][]{
{5, 4, 3, 2, 1, 0},
{54, 35, 48, 36, 27, 12, 44, 44, 8, 14, 26, 17, 28},
{32, 103, 24, 88, 95, 70, 97, 15, 102, 6, 79, 46, 51, 37, 93, 108, 9, 58, 53, 58, 79, 36, 58, 91, 78, 58, 61, 81}
};
public static void main(String[] args) {
randomDemoArr();
sort(new DemoSort1());
// sortAll();
}
private static void sortAll() {
sort(new BubbleSort1());
sort(new BubbleSort2());
sort(new BubbleSort3());
sort(new HeapSort1());
sort(new InsertSort1());
sort(new InsertSort2());
sort(new MergeSort1());
sort(new QuickSort1());
sort(new QuickSort2());
sort(new SelectSort1());
sort(new ShellSort1());
sort(new ShellSort2());
}
private static void sort(IKySort sorter) {
log("\n\n\n\n\n\n排序开始: " + sorter.getClass().getSimpleName());
for (int[] arr : demoArr) {
int[] expected = Arrays.copyOf(arr, arr.length);
Arrays.sort(expected);
if (doSort(sorter, expected, arr)) {
log("成功" + display("", expected));
} else {
return;
}
}
}
private static boolean doSort(IKySort sorter, int[] expected, int[] arr) {
int[] sorted = Arrays.copyOf(arr, arr.length);
sorter.sort(sorted, sorted.length);
if (!Arrays.equals(expected, sorted)) {
log("\n---- " + "排序异常: " + sorter.getClass().getSimpleName() + " ----");
log(display("sorted", sorted));
log(display("expected", expected));
log(display("actual", arr));
throw new RuntimeException();
// return false;
}
return true;
}
private static String display(String prefix, int[] arr) {
StringBuilder builder = new StringBuilder();
builder.append(prefix).append(": ");
for (int i : arr) {
builder.append(i).append(" ");
}
return builder.toString();
}
private static void log(String str) {
System.out.println(str);
}
private static void randomDemoArr() {
List<int[]> list = new ArrayList<>();
for (int i = 0; i < 100; i++) {
int size = ThreadLocalRandom.current().nextInt(1, 30);
int[] arr = new int[size];
list.add(arr);
for (int j = 0; j < size; j++) {
arr[j] = ThreadLocalRandom.current().nextInt(100);
}
}
demoArr = list.toArray(new int[0][0]);
}
}