-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSortingHelper.java
30 lines (23 loc) · 980 Bytes
/
SortingHelper.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
public class SortingHelper {
private SortingHelper(){}
public static <E extends Comparable<E>> boolean isSorted(E[] arr){
for(int i = 1; i < arr.length; i ++)
if(arr[i - 1].compareTo(arr[i]) > 0)
return false;
return true;
}
public static <E extends Comparable<E>> void sortTest(String sortname, E[] arr){
long startTime = System.nanoTime();
if(sortname.equals("SelectionSort"))
SelectionSort.sort(arr);
else if(sortname.equals("InsertionSort"))
InsertionSort.sort(arr);
else if(sortname.equals("InsertionSort2"))
InsertionSort.sort2(arr);
long endTime = System.nanoTime();
double time = (endTime - startTime) / 1000000000.0;
if(!SortingHelper.isSorted(arr))
throw new RuntimeException(sortname + " failed");
System.out.println(String.format("%s , n = %d : %f s", sortname, arr.length, time));
}
}