-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
228 lines (173 loc) · 8.31 KB
/
Copy pathMain.java
File metadata and controls
228 lines (173 loc) · 8.31 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import java.util.Scanner;
import java.util.Random;
import SortingAlgorithms.*;
public class Main {
public static void main(String []args){
//Creating Objects from the Algorithms Classes;
QuickSort qs = new QuickSort();
Scanner sc = new Scanner(System.in);
System.out.print("Digite o tamanho da amostra desejada (tamanho dos arrays): ");
int arraysSize = sc.nextInt();
sc.close();
int []arrayAlmostSorted = new int[arraysSize];
int []arraySortedDecreased = new int[arraysSize];
int []arrayNotSorted = new int[arraysSize];
Random gen = new Random();
for(int i = 0; i < arraysSize; i++){
int currNum = gen.nextInt(arraysSize * 3);
arrayNotSorted[i] = currNum;
arrayAlmostSorted[i] = currNum;
arraySortedDecreased[i] = currNum;
}
//Quicksort used to Sort the arrays but this part will not be
//considered in the comparison of algorithms
qs.quicksortDESC(arraySortedDecreased, 0, arraySortedDecreased.length - 1);
qs.quicksort(arrayAlmostSorted,0,arrayAlmostSorted.length-1);
//Parameter bound is exclusive,
// so it chooses a number from 0 to 5
int randomPercentage = gen.nextInt(6) + 5;
//For loop created to set 5 - 10 % of the array to not sorted
for(int i = 0; i < randomPercentage; i++){
int first = gen.nextInt(arraysSize);
int last = gen.nextInt(arraysSize);
int temp = arrayAlmostSorted[first];
arrayAlmostSorted[first] = arrayAlmostSorted[last];
arrayAlmostSorted[last] = temp;
}
testAlgorithms(1, arrayAlmostSorted, arraySortedDecreased, arrayNotSorted);
testAlgorithms(2, arrayAlmostSorted, arraySortedDecreased, arrayNotSorted);
testAlgorithms(3, arrayAlmostSorted, arraySortedDecreased, arrayNotSorted);
testAlgorithms(4, arrayAlmostSorted, arraySortedDecreased, arrayNotSorted);
testAlgorithms(5, arrayAlmostSorted, arraySortedDecreased, arrayNotSorted);
testAlgorithms(6, arrayAlmostSorted, arraySortedDecreased, arrayNotSorted);
}
//The parameter algorithm will define which algorithm will be used to do the Sorting
public static void testAlgorithms(int algorithm, int[] arrayAlmostSorted,
int[] arrayDesc, int[]arrayNotSorted){
//Creating a clone for the arrays
int[] copyAlmostSorted = arrayAlmostSorted.clone();
int[] copyArrayDesc = arrayDesc.clone();
int[] copyArrayNotSorted = arrayNotSorted.clone();
if(algorithm == 1){
//QuickSort testing
QuickSort qs = new QuickSort();
double almostSortedTimer, descTimer, notSortedTimer, startTimer, endTimer;
startTimer = System.currentTimeMillis();
qs.quicksort(copyAlmostSorted, 0, copyAlmostSorted.length-1);
endTimer = System.currentTimeMillis();
almostSortedTimer = (endTimer - startTimer)/1000;
startTimer = System.currentTimeMillis();
qs.quicksort(copyArrayDesc,0,copyArrayDesc.length-1);
endTimer = System.currentTimeMillis();
descTimer = (endTimer - startTimer)/1000;
startTimer = System.currentTimeMillis();
qs.quicksort(copyArrayNotSorted,0,copyArrayNotSorted.length-1);
endTimer = System.currentTimeMillis();
notSortedTimer = (endTimer - startTimer)/1000;
System.out.println("--- QuickSort ---");
System.out.println("Almost Sorted Array: "+almostSortedTimer+"s");
System.out.println("Descending Order Array: "+descTimer+"s");
System.out.println("Not Sorted Array: "+notSortedTimer+"s");
}
else if (algorithm == 2) {
// Testando o ShellSort
ShellSort ss = new ShellSort();
double almostSortedTimer, descTimer, notSortedTimer, startTimer, endTimer;
startTimer = System.currentTimeMillis();
ss.shellSortASC(copyAlmostSorted);
endTimer = System.currentTimeMillis();
almostSortedTimer = (endTimer - startTimer) / 1000.0;
startTimer = System.currentTimeMillis();
ss.shellSortASC(copyArrayDesc);
endTimer = System.currentTimeMillis();
descTimer = (endTimer - startTimer) / 1000.0;
startTimer = System.currentTimeMillis();
ss.shellSortDESC(copyArrayNotSorted);
endTimer = System.currentTimeMillis();
notSortedTimer = (endTimer - startTimer) / 1000.0;
System.out.println("\n--- ShellSort ---");
System.out.println("Almost Sorted Array: " + almostSortedTimer + "s");
System.out.println("Descending Order Array: " + descTimer + "s");
System.out.println("Not Sorted Array: " + notSortedTimer + "s");
}
else if (algorithm == 3) {
//Testing CockTail Shaker Sort
CocktailShakerSort css = new CocktailShakerSort();
double almostSortedTimer, descTimer, notSortedTimer, startTimer, endTimer;
startTimer = System.currentTimeMillis();
css.cocktailShakerSort(copyAlmostSorted);
endTimer = System.currentTimeMillis();
almostSortedTimer = (endTimer - startTimer) / 1000.0;
startTimer = System.currentTimeMillis();
css.cocktailShakerSort(copyArrayDesc);
endTimer = System.currentTimeMillis();
descTimer = (endTimer - startTimer) / 1000.0;
startTimer = System.currentTimeMillis();
css.cocktailShakerSort(copyArrayNotSorted);
endTimer = System.currentTimeMillis();
notSortedTimer = (endTimer - startTimer) / 1000.0;
System.out.println("\n--- CockTail Shaker Sort ---");
System.out.println("Almost Sorted Array: " + almostSortedTimer + "s");
System.out.println("Descending Order Array: " + descTimer + "s");
System.out.println("Not Sorted Array: " + notSortedTimer + "s");
}
else if (algorithm == 4) {
Heapsort hs = new Heapsort();
double almostSortedTimer, descTimer, notSortedTimer, startTimer, endTimer;
startTimer = System.currentTimeMillis();
hs.heapsort(copyAlmostSorted);
endTimer = System.currentTimeMillis();
almostSortedTimer = (endTimer - startTimer) / 1000.0;
startTimer = System.currentTimeMillis();
hs.heapsort(copyArrayDesc);
endTimer = System.currentTimeMillis();
descTimer = (endTimer - startTimer) / 1000.0;
startTimer = System.currentTimeMillis();
hs.heapsort(copyArrayNotSorted);
endTimer = System.currentTimeMillis();
notSortedTimer = (endTimer - startTimer) / 1000.0;
System.out.println("\n--- Heapsort ---");
System.out.println("Almost Sorted Array: " + almostSortedTimer + "s");
System.out.println("Descending Order Array: " + descTimer + "s");
System.out.println("Not Sorted Array: " + notSortedTimer + "s");
} else if (algorithm == 5) {
MergeSort ms = new MergeSort();
double almostSortedTimer, descTimer, notSortedTimer, startTimer, endTimer;
startTimer = System.currentTimeMillis();
ms.mergeSort(copyAlmostSorted, 0, (copyAlmostSorted.length - 1));
endTimer = System.currentTimeMillis();
almostSortedTimer = (endTimer - startTimer) / 1000.0;
startTimer = System.currentTimeMillis();
ms.mergeSort(copyArrayDesc, 0, (copyArrayDesc.length - 1));
endTimer = System.currentTimeMillis();
descTimer = (endTimer - startTimer) / 1000.0;
startTimer = System.currentTimeMillis();
ms.mergeSort(copyArrayNotSorted, 0, (copyArrayNotSorted.length - 1));
endTimer = System.currentTimeMillis();
notSortedTimer = (endTimer - startTimer) / 1000.0;
System.out.println("\n--- Merge Sort ---");
System.out.println("Almost Sorted Array: " + almostSortedTimer + "s");
System.out.println("Descending Order Array: " + descTimer + "s");
System.out.println("Not Sorted Array: " + notSortedTimer + "s");
} else if (algorithm == 6) {
InsertionSort is = new InsertionSort();
double almostSortedTimer, descTimer, notSortedTimer, startTimer, endTimer;
startTimer = System.currentTimeMillis();
is.insertionSort(copyAlmostSorted);
endTimer = System.currentTimeMillis();
almostSortedTimer = (endTimer - startTimer) / 1000.0;
startTimer = System.currentTimeMillis();
is.insertionSort(copyArrayDesc);
endTimer = System.currentTimeMillis();
descTimer = (endTimer - startTimer) / 1000.0;
startTimer = System.currentTimeMillis();
is.insertionSort(copyArrayNotSorted);
endTimer = System.currentTimeMillis();
notSortedTimer = (endTimer - startTimer) / 1000.0;
System.out.println("\n--- Insertion Sort ---");
System.out.println("Almost Sorted Array: " + almostSortedTimer + "s");
System.out.println("Descending Order Array: " + descTimer + "s");
System.out.println("Not Sorted Array: " + notSortedTimer + "s");
}
}
}