Skip to content

Commit 593c006

Browse files
committed
testing radix sort.
1 parent c826866 commit 593c006

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed

src/main/java/com/example/Bench.java

+14-2
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,13 @@ public static interface Function<A, B> {
106106
}
107107
};
108108

109+
public static Function<int[], int[]> radixSort = new Function<int[], int[]>() {
110+
@Override public int[] apply(int[] array) {
111+
SortAlgorithms.radixSort(array);
112+
return array;
113+
}
114+
};
115+
109116
// Execute an algorithm on an input and return its runtime.
110117
private static String execute(Function<int[], int[]> algorithm, int[] input) {
111118
// To get accurate results even for small inputs, we repeat
@@ -208,7 +215,8 @@ private static void executionTimeReport(int size) {
208215
"Heap sort | %14s | %14s | %14s\n" +
209216
"Bubble sort | %14s | %14s | %14s\n" +
210217
"Quicksort | %14s | %14s | %14s\n" +
211-
"Merge sort | %14s | %14s | %14s\n",
218+
"Merge sort | %14s | %14s | %14s\n" +
219+
"Radix sort | %14s | %14s | %14s\n",
212220
size,
213221
"Random", "95% sorted", "Sorted",
214222
execute(insertionSort, randomSample),
@@ -237,7 +245,11 @@ private static void executionTimeReport(int size) {
237245

238246
execute(mergeSort, randomSample),
239247
execute(mergeSort, partiallySortedSample),
240-
execute(mergeSort, sortedSample)
248+
execute(mergeSort, sortedSample),
249+
250+
execute(radixSort, randomSample),
251+
execute(radixSort, partiallySortedSample),
252+
execute(radixSort, sortedSample)
241253
));
242254
}
243255
}

src/main/java/com/example/SortAlgorithms.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
*
2020
*/
2121
class SortAlgorithms {
22-
public static final boolean ENABLE_PRINT = true;
22+
public static final boolean ENABLE_PRINT = false;
2323

2424
public static void main(String[] args) {
2525
// int[] array = new int[]{3,5,3,0,8,6,1,5,8,6,2,4,9,4,7,0,1,8,9,7,3,1,2,5,9,7,4,0,2,6};
@@ -319,7 +319,7 @@ public static void radixSort(int[] arr){
319319
System_out_println("maxDigit: " + maxDigit);
320320

321321
//申请一个桶空间
322-
int[][] buckets = new int[10][arr.length-1];
322+
int[][] buckets = new int[10][arr.length];
323323
int base = 10;
324324

325325
//从低位到高位,对每一位遍历,将所有元素分配到桶中

src/main/java/com/example/Test.java

+9-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,15 @@
88
public class Test {
99
private static Bench.Function<int[], int[]> algorithm =
1010
// Change to Bench.quickSort or Bench.mergeSort as appropriate.
11-
Bench.shellSort; // 需要测试的排序算法
11+
Bench.insertionSort; //直接插入排序 // 需要测试的排序算法
12+
// Bench.shellSort; //希尔排序
13+
// Bench.selectionSort; //简单选择排序
14+
// Bench.heapSort; //堆排序
15+
// Bench.bubbleSort; //冒泡排序
16+
// Bench.quickSort; //快速排序
17+
// Bench.mergeSort; //归并排序
18+
// Bench.radixSort; //基数排序
19+
1220

1321
private static boolean check(int[] array) {
1422
int[] reference = Arrays.copyOf(array, array.length);

0 commit comments

Comments
 (0)