-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenericSortingTest.java
More file actions
128 lines (105 loc) · 4.12 KB
/
GenericSortingTest.java
File metadata and controls
128 lines (105 loc) · 4.12 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
/* ************************************************
* Classe per testare gli algoritmi di ordinamento
* su tipi generici.
*
* Esempio di esecuzione::
*
* java GenericSortingTest file selectionsort
*
* dove "file" è il nome di un file contenente interi
* e selectionsort è uno degli algoritmi di ordinamento
* fornini dalla classe Sorting.
*
* *************************************************/
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.lang.reflect.Method;
import java.util.Comparator;
import algorithm.sorting.Sorting;
public class GenericSortingTest {
private static Integer[] readarray(String filename) throws FileNotFoundException {
ArrayList<Integer> L = new ArrayList<Integer>();
Scanner input = new Scanner(new File(filename));
while(input.hasNext())
L.add(input.nextInt());
return L.toArray(new Integer[L.size()]);
}
private static Integer[] savesorted(Integer A[]) {
Integer[] B = A.clone();
Comparator<Integer> comparator = Integer::compare;
Arrays.sort(B,comparator);
return B;
}
private static boolean issorted(Integer A[], Integer B[]) {
for(int i = 0; i < A.length-1; i++)
if(A[i].intValue() != B[i].intValue()) {
System.out.println("\nDIFF at " + i + " " + A[i] + " " + B[i]);
System.out.println(Arrays.toString(A));
System.out.println(Arrays.toString(B));
System.exit(0);
return false;
}
return true;
}
public static void main(String args[]) {
Integer A[] = null;
Integer B[] = null;
Integer C[] = null;
long start, end;
if(args.length != 1) {
System.err.println("Usage: GenericSortingTest <filename>");
System.exit(0);
}
try {
A = readarray(args[0]);
B = savesorted(A);
C = Arrays.copyOf(A,A.length);
} catch(Exception e) {
System.out.println(e.toString());
System.exit(1);
}
try {
/* // Selectionsort
System.out.print("Algorithm: selectionsort ");
start = System.currentTimeMillis();
Sorting.selectionsort(A);
end = System.currentTimeMillis();
System.out.println("Time: " + (end-start)/1000.0 + " sec " + (issorted(A,B) ? "[SORTED]" : "[UNSORTED]"));
// Insertionsort
System.out.print("Algorithm: insertionsort ");
A = Arrays.copyOf(C,C.length);
start = System.currentTimeMillis();
Sorting.insertionsort(A);
end = System.currentTimeMillis();
System.out.println("Time: " + (end-start)/1000.0 + " sec " + (issorted(A,B) ? "[SORTED]" : "[UNSORTED]"));
*/
// Mergesort
System.out.print("Algorithm: mergesort ");
A = Arrays.copyOf(C,C.length);
start = System.currentTimeMillis();
Sorting.mergesort(A);
end = System.currentTimeMillis();
System.out.println("Time: " + (end-start)/1000.0 + " sec " + (issorted(A,B) ? "[SORTED]" : "[UNSORTED]"));
// Quicksort
System.out.print("Algorithm: quicksort ");
A = Arrays.copyOf(C,C.length);
start = System.currentTimeMillis();
Sorting.quicksort(A);
end = System.currentTimeMillis();
System.out.println("Time: " + (end-start)/1000.0 + " sec " + (issorted(A,B) ? "[SORTED]" : "[UNSORTED]"));
// Heapsort
System.out.print("Algorithm: heapsort ");
A = Arrays.copyOf(C,C.length);
start = System.currentTimeMillis();
Sorting.heapsort(A);
end = System.currentTimeMillis();
System.out.println("Time: " + (end-start)/1000.0 + " sec " + (issorted(A,B) ? "[SORTED]" : "[UNSORTED]"));
} catch(Exception e) {
e.printStackTrace();
System.exit(0);
}
}
}