-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortTestHelper.java
More file actions
142 lines (119 loc) · 4.25 KB
/
Copy pathSortTestHelper.java
File metadata and controls
142 lines (119 loc) · 4.25 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
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
136
137
138
139
140
141
package demo;
/**
* Created by Administrator on 2018/3/28.
* 这是一个排序算法的辅助类,包括:
* 1、生成随机数组;
* 2、生成近似有序的数组;
* 3、输出数组;
* 4、判断数组是否有序;
* 5、测试排序算法的时间;
* 6、统计一个字符串中的字符个数;
* 7、将一个字符串数组转化为字符串;
*/
import java.lang.reflect.Method;
/**
* Created by Administrator on 2018/3/23.
*/
public class SortTestHelper {
//生成有n个元素的随机数组,每个元素的随机范围为rangeL,rangeR
public static Comparable[] generateRandomArray(int n,int rangeL,int rangeR){
assert rangeL<=rangeR;
Integer[] arr = new Integer[n];
for (int i = 0; i < n; i++) {
arr[i] = new Integer((int) (Math.random()*(rangeR-rangeL)+rangeL));
// arr[i] = (int)Math.random()*(rangeR - rangeL)+rangeL;
}
return arr;
}
/**
* 生成一个几乎有序的数组。
* 1、首先生成一个完全有序的数组;
* 2、随机挑选几对数字进行交换;
* */
public static Integer[] generateNearlyRandomArray(int n,int swapTimes){
Integer[] arr = new Integer[n];
for (int i = 0; i < n; i++) {
arr[i] = new Integer(i);
}
//(int) (Math.random() * n) = m
//(int) Math.random() * n = 0
for (int i = 0; i < swapTimes; i++) {
int a = (int) (Math.random() * n);
int b = (int) (Math.random() * n);
//Math.random()范围是[0,1)Math.random() * n是为了使得到的随机数的范围在0 < a < n之间;
int t = arr[a];
arr[a] = arr[b];
arr[b] = t;
}
return arr;
}
// public static void swap(int[] arr,int i,int j){
// int t = arr[i];
// arr[i] = arr[j];
// arr[j] = t;
// }
public static void printArray(Comparable arr[]){
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]);
System.out.print(' ');
}
System.out.println();
}
//测试算法的排序正确性
public static boolean isSorted(Comparable[] arr){
for (int i = 0; i < arr.length; i++)
if (arr[i].compareTo(arr[i+1])>0)
return false;
return true;
}
//测试算法的运行时间
public static void testSort(String sortName,Comparable[] arr){
try {
//使用java的反射机制通过类名获得具体的类
Class sortClass = Class.forName(sortName);
//使用类中的排序方法
Method sortMethod = sortClass.getMethod("sort",new Class[]{Comparable[].class});
Object[] params = new Object[]{arr};
long startTime = System.currentTimeMillis();
sortMethod.invoke(null,params);
long endTime = System.currentTimeMillis();
/**
* 断言方法,在调试测试程序时使用,一般使用if方法;
if (isSorted(arr)){
System.out.println(sortClass.getSimpleName()+":"+(endTime-startTime)+"ms");
}
*/
assert isSorted(arr);
System.out.println(sortClass.getSimpleName()+":"+(float)(endTime-startTime)/1000+"s");
} catch (Exception e) {
e.printStackTrace();
}
}
//统计一个字符串中有哪些元素并打印出元素的个数;
public static void count(String value){
int n = value.length();
char[] arrChar = value.toCharArray();
int[] arrASCII = new int[n];
for (int i = 0; i < n; i++) {
arrASCII[i] = arrChar[i];
}
for (int i = 65;i < 123;i++){
int count = 0;
for (int j = 0; j < n; j++) {
if (arrASCII[j] == i){
count++;
}
}
if (count!=0)
System.out.println("字母"+ ((char) i)+"有:"+count+"个");
}
}
//将一个字符串数组变为字符串;
public static String StringArrayToString(String[] arr){
String result = "";
for (int i = 0;i < arr.length;i++){
result = result+arr[i];
}
return result;
}
}