-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDriver.java
More file actions
253 lines (198 loc) · 7.67 KB
/
Driver.java
File metadata and controls
253 lines (198 loc) · 7.67 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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class Driver {
public static Scanner sc = new Scanner(System.in);
public static void main(String[] args){
int input;
System.out.println(writeListState(InputGenerator.generateWorstCaseMerge(100)));
do{
System.out.println("0 - Exit");
System.out.println("1 - Input values");
System.out.println("2 - Generate random values");
System.out.println("3 - Read input from an input file");
input = sc.nextInt();
if(input != 0){
switch(input){
case 1:
enterInputManually();
break;
case 2:
enterInputUsingRandom();
break;
case 3:
enterInputUsingFile();
default:
}
}
}while(input != 0);
}
public static void enterInputManually(){
NumberList list = null;
System.out.println("Enter all values to be sorted. Separate them with a comma (','). End your input by pressing ENTER.");
sc.nextLine();
String values = sc.nextLine();
list = createListFromString(values);
System.out.println("Entered " + writeListState(list));
solve(list);
}
public static void enterInputUsingRandom(){
NumberList list = null;
System.out.print("Enter how many values will be randomly generated (MAX SIZE OF AN ARRAYLIST IS " + Integer.MAX_VALUE + " : ");
int size = sc.nextInt();
list = InputGenerator.generateRandomInput(size);
solve(list);
}
public static void enterInputUsingFile(){
TextFileChooser chooser = new TextFileChooser();
int userChoice = chooser.showDialog(null, "Select");
TextFileReader reader = new TextFileReader();
String output = reader.read(chooser.getSelectedFile().getAbsolutePath());
String[] rawData = output.split("\n");
NumberList[] processedInput = new NumberList[rawData.length];
for(int i = 0; i < rawData.length; i++){
processedInput[i] = createListFromString(rawData[i]);
System.out.println("[" + (i+1) + "] " + writeListState(processedInput[i], 10));
}
if(rawData.length > 1){
System.out.println("[" + (rawData.length+1) + "] Sort All");
}
System.out.println();
System.out.println("Which list number do you wish to sort");
int toSort = sc.nextInt();
System.out.println("INPUT RETURNED IS " + toSort);
if(toSort <= processedInput.length){
toSort-=1;
solve(processedInput[toSort]);
}else{
System.out.println("SOLVING ALL");
for(int i = 0; i < processedInput.length; i++){
System.out.println("LINE NUMBER " + i);
solve(processedInput[i]);
}
}
}
private static void solve(NumberList list){
int sizeToCheck;
NumberList listForStepsSelection = new NumberList();
NumberList listForStepsMerge = new NumberList();
SortingAlgorithm mergeSort = new MergeSort();
SortingAlgorithm selectionSort = new SelectionSort();
NanoTimer mergeSortTimer = new NanoTimer();
NanoTimer selectionSortTimer = new NanoTimer();
NumberList mergeSorted;
long mergeSortedFrequencyCount;
Result mergeSortedResults;
NumberList selectionSorted;
Result selectionSortedResults;
long selectionSortedFrequencyCount;
sizeToCheck = list.size();
listForStepsSelection.addAll(list);
listForStepsMerge.addAll(list);
String log = "";
log += "GENERATED LIST TO SORT: \n";
log += "\t" + writeListState(list) + "\n";
mergeSortTimer.start();
mergeSorted = mergeSort.sort(list);
mergeSortTimer.stop();
mergeSortedFrequencyCount = MergeSortResultRecorder.getInstance().getResult().getTotalFreqCount();
selectionSortTimer.start();
selectionSorted = selectionSort.sort(list);
selectionSortTimer.stop();
selectionSortedFrequencyCount = ((SelectionSort)selectionSort).getTotalFC();
if(sizeToCheck <= 100){
System.out.println("LISTING DOWN STEPS...");
mergeSortedResults = mergeSort.sortWithSteps(listForStepsSelection);
selectionSortedResults = selectionSort.sortWithSteps(listForStepsMerge);
}else{
mergeSortedResults = null;
selectionSortedResults = null;
}
log += "\n=========================================================================================================================\n";
log += "RESULTS:";
log += "\n=========================================================================================================================\n";
log += "ORDERED LISTS:\n";
log += "\nSELECTION SORT:\n";
log += "\t" + writeListState(selectionSorted) + "\n";
log += "\nMERGE SORT:\n";
log += "\t" + writeListState(mergeSorted) + "\n";
log += "\nEXECUTION TIME:\n";
log += "\tSELECTION SORT: " + selectionSortTimer.getFormattedTimeLapsed() + "\n";
log += "\tMERGE SORT : " + mergeSortTimer.getFormattedTimeLapsed() + "\n\n";
log += "\nFREQUENCY COUNT:\n";
log += "\tSELECTION SORT : " + selectionSortedFrequencyCount + "\n";
log += "\tMERGE SORT : " + mergeSortedFrequencyCount + "\n\n";
if(sizeToCheck <= 100){
log += "\n=========================================================================================================================\n";
log += "STEPS FOR SELECTION SORT:";
log += "\n=========================================================================================================================\n";
for(int i = 0; i < selectionSortedResults.getSteps().size(); i++){
log += selectionSortedResults.getSteps().get(i) + "\n";
}
log += "\n=========================================================================================================================\n";
log += "STEPS FOR MERGE SORT:";
log += "\n=========================================================================================================================\n";
for(int i = 0; i < mergeSortedResults.getSteps().size(); i++){
log += mergeSortedResults.getSteps().get(i) + "\n";
}
}else{
log += "Steps not recorded. List size is too big.";
}
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("YYYYMMdd_HHmmss");
String filename = "result_" + sdf.format(date);
filename = generateFilename(filename, ".txt");
System.out.print("\n" + log);
if(TextFileWriter.save(filename, log)){
System.out.println("Results saved under \"" + new File(filename).getAbsolutePath() + "\"");
}else{
System.out.println("Problems 1encountered when saving results under " + filename + ". Saving aborted.");
}
System.out.println();
}
private static String writeListState(NumberList list){
String listString = "[";
for(int i = 0; i < list.size(); i++){
listString += "" + list.get(i) + ", ";
}
listString = listString.substring(0, listString.length()-2);
listString += "]";
return listString;
}
private static String writeListState(NumberList list, int limit){
if(list.size() <= limit){
return writeListState(list);
}else{
String listString = "[";
for(int i = 0; i < limit; i++){
listString += list.get(i) + ", ";
}
listString = listString.substring(0, listString.length()-1);
listString += " ... and " + (list.size()-limit) + " more numbers]";
return listString;
}
}
public static NumberList createListFromString(String values){
NumberList list = new NumberList();
String[] rawValues = values.split(",");
for(int i = 0; i < rawValues.length; i++){
list.add(Integer.valueOf(rawValues[i].trim()));
}
return list;
}
public static String generateFilename(String filename, String extension){
String append;
int value = -1;
File file;
boolean isExisting = true;
do{
value++;
append = value == 0 ? "" : "_" + value;
System.out.println("TRYING " + filename + append + extension);
file = new File(filename + append + extension);
isExisting = file.exists();
}while(isExisting);
return filename+append+extension;
}
}