-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathArraySortTest.java
135 lines (111 loc) · 3.77 KB
/
ArraySortTest.java
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.util.Arrays;
import java.util.Random;
import static org.junit.Assert.*;
/**
* Created by corning on 2017/12/19.
*/
public class ArraySortTest {
private int[] array;
private int[] sortedArray;
// 计数排序等不支持负数排序
private int[] positiveArray;
private int[] positiveArraySorted;
@Before
public void setUp() throws Exception {
// 生成随机数组
array = randomArray(-1000, 1000, 100);
// 使用 Arrays.sort() 排序作为对比
sortedArray = Arrays.copyOf(array, array.length);
Arrays.sort(sortedArray);
positiveArray = randomArray(0, 1000, 100);
positiveArraySorted = Arrays.copyOf(positiveArray, positiveArray.length);
Arrays.sort(positiveArraySorted);
}
/**
* 随机指定范围内N个不重复的数
* 在初始化的无重复待选数组中随机产生一个数放入结果中,
* 将待选数组被随机到的数,用待选数组(len-1)下标对应的数替换
* 然后从len-2里随机产生下一个随机数,如此类推
*
* @param max 指定范围最大值
* @param min 指定范围最小值
* @param n 随机数个数
* @return int[] 随机数结果集
*/
public int[] randomArray(int min, int max, int n) {
int len = max - min + 1;
if (max < min || n > len) {
return null;
}
//初始化给定范围的待选数组
int[] source = new int[len];
for (int i = min; i < min + len; i++) {
source[i - min] = i;
}
int[] result = new int[n];
Random rd = new Random();
int index = 0;
for (int i = 0; i < result.length; i++) {
//待选数组0到(len-2)随机一个下标
index = Math.abs(rd.nextInt() % len--);
//将随机到的数放入结果集
result[i] = source[index];
//将待选数组中被随机到的数,用待选数组(len-1)下标对应的数替换
source[index] = source[len];
}
return result;
}
@After
public void tearDown() throws Exception {
array = null;
sortedArray = null;
}
@Test
public void bubbleSort() throws Exception {
assertArrayEquals(sortedArray, new BubbleSort().sort(array));
}
@Test
public void choiceSort() throws Exception {
assertArrayEquals(sortedArray, new SelectionSort().sort(array));
}
@Test
public void insertSort() throws Exception {
assertArrayEquals(sortedArray, new InsertSort().sort(array));
}
@Test
public void shellSort() throws Exception {
assertArrayEquals(sortedArray, new ShellSort().sort(array));
}
@Test
public void mergeSort() throws Exception {
assertArrayEquals(sortedArray, new MergeSort().sort(array));
}
@Test
public void quickSort() throws Exception {
assertArrayEquals(sortedArray, new QuickSort().sort(array));
}
@Test
public void heapSort() throws Exception {
assertArrayEquals(sortedArray, new HeapSort().sort(array));
}
@Test
public void countingSort() throws Exception {
assertArrayEquals(positiveArraySorted, new CountingSort().sort(positiveArray));
}
@Test
public void bucketSort() throws Exception {
assertArrayEquals(sortedArray, new BucketSort().sort(array));
}
@Test
public void radixSort() throws Exception {
assertArrayEquals(sortedArray, new RadixSort().sort(array));
}
@Test
public void radixSort_getNumLenght() throws Exception {
assertEquals(3, new RadixSort().getNumLenght(-100));
assertEquals(1, new RadixSort().getNumLenght(1));
}
}