-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlgortihmsFX.java
More file actions
463 lines (381 loc) · 19.9 KB
/
AlgortihmsFX.java
File metadata and controls
463 lines (381 loc) · 19.9 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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package algortihmsfx;
import java.util.Arrays;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.stage.Stage;
/**
*
* @author Ryan
*/
public class AlgortihmsFX extends Application {
Stage window;
Scene menuScene, selectSortScene, mergeSortScene, bubbleSortScene,
recursiveBubbleSortScene, heapSortScene, timSortScene, binarySearchScene,
linearSearchScene, jumpSearchScene, interSearchScene, exponentialSearchScene,
sublistSearchScene, fiboSearchScene;
final int WIDTH = 500, HEIGHT = 400;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
int[] arr = { 1,4,6,8,10,12,16,18,20,33 };
int result = Algorithms.interpolationSearch(arr,12);
System.out.println(result);
launch(args);
}
@Override
public void start(Stage primaryStage) {
window = primaryStage;
/* Main screen layouts */
/*
Sorting algorithms layout
*/
Label sortingLabel = new Label("Sorting");
Button selectSortBtn = new Button("Selection sort");
Button mergeSortBtn = new Button("Merge sort");
Button bubbleSortBtn = new Button("Bubble sort");
Button recursiveBubbleSortBtn = new Button("Recursive bubble sort");
//Button heapSortBtn = new Button("Heap sort");
//Button timSortBtn = new Button("Tim sort");
selectSortBtn.setOnAction( e -> window.setScene(selectSortScene) );
mergeSortBtn.setOnAction( e -> window.setScene(mergeSortScene) );
bubbleSortBtn.setOnAction( e -> window.setScene(bubbleSortScene) );
recursiveBubbleSortBtn.setOnAction( e -> window.setScene(recursiveBubbleSortScene) );
//heapSortBtn.setOnAction( e -> window.setScene(heapSortScene) );
//timSortBtn.setOnAction( e -> window.setScene(timSortScene) );
VBox sortingLayout = new VBox(10);
sortingLayout.getChildren().addAll(sortingLabel, selectSortBtn, mergeSortBtn,
bubbleSortBtn, recursiveBubbleSortBtn);
/*
Searching algorithms layout
*/
// Buttons and labels
Label searchingLabel = new Label("Searching");
Button linearSearchBtn = new Button("Linear search");
Button binarySearchBtn = new Button("Binary search");
Button jumpSearchBtn = new Button("Jump search");
Button interSearchBtn = new Button("Interpolation search");
//Button sublistSearchBtn = new Button("Sublist search");
//Button fiboSearchBtn = new Button("Fibonacci search");
linearSearchBtn.setOnAction( e -> window.setScene(linearSearchScene) );
binarySearchBtn.setOnAction( e -> window.setScene(binarySearchScene) );
jumpSearchBtn.setOnAction( e -> window.setScene(jumpSearchScene) );
interSearchBtn.setOnAction( e -> window.setScene(interSearchScene) );
//sublistSearchBtn.setOnAction( e -> window.setScene(sublistSearchScene) );
//fiboSearchBtn.setOnAction( e -> window.setScene(fiboSearchScene) );
VBox searchingLayout = new VBox(10);
searchingLayout.getChildren().addAll(searchingLabel, linearSearchBtn,
binarySearchBtn, jumpSearchBtn, interSearchBtn);
// Main screen Layout
HBox layout = new HBox(40);
layout.setPadding(new Insets(20,20,20,20));
layout.getChildren().addAll(sortingLayout, searchingLayout);
/*
Internal layouts
*/
// ---- Sorting Algorithms ----
// Select sort layout
Label selectSortLabel = new Label("Select sort");
Label output = new Label();
Text inputPrompt = new Text("Enter a list of integers seperated by spaces:");
TextField inputText = new TextField();
Button submitBtn = new Button("Submit");
Button backBtnSelect = new Button("Back");
backBtnSelect.setOnAction( e -> window.setScene(menuScene) );
submitBtn.setOnAction(e -> {
if( validateArray(inputText.getText()) ) {
int[] arr = parseArray(inputText.getText());
double startTime = System.nanoTime();
int[] sortedArr = Algorithms.selectionSort(arr);
double endTime = System.nanoTime();
double elapsedTime = (endTime - startTime)/1000;
output.setText("Sorted Array: " + Arrays.toString(sortedArr) +
"\nExecuted in " + elapsedTime + " microseconds. ");
} else {
output.setText("Invalid input. Please try again");
}
});
VBox selectSortLayout = new VBox(10);
selectSortLayout.setPadding(new Insets(20,20,20,20));
selectSortLayout.getChildren().addAll(selectSortLabel, inputPrompt, inputText,
submitBtn, output, backBtnSelect);
/*
Merge sort layout
*/
Label mergeSortLabel = new Label("Merge sort");
Button backBtnMerge = new Button("Back");
Label outputMerge = new Label();
Text inputPromptMerge = new Text("Enter a list of integers seperated by spaces:");
TextField inputTextMerge = new TextField();
backBtnMerge.setOnAction( e -> window.setScene(menuScene) );
Button submitBtnMerge = new Button("Submit");
submitBtnMerge.setOnAction(e -> {
if( validateArray(inputTextMerge.getText()) ) {
int[] arr = parseArray(inputTextMerge.getText());
double startTime = System.nanoTime();
int[] sortedArr = Algorithms.mergeSort(arr, 0, arr.length-1);
double endTime = System.nanoTime();
double elapsedTime = (endTime - startTime)/1000;
outputMerge.setText("Sorted Array: " + Arrays.toString(sortedArr) +
"\nExecuted in " + elapsedTime + " microseconds. ");
} else {
outputMerge.setText("Invalid input. Please try again");
}
});
// Layout
VBox mergeSortLayout = new VBox(10);
mergeSortLayout.setPadding(new Insets(20,20,20,20));
mergeSortLayout.getChildren().addAll(mergeSortLabel, inputPromptMerge,
inputTextMerge, submitBtnMerge, outputMerge, backBtnMerge);
/*
Bubble sort layout
*/
Label bubbleSortLabel = new Label("Bubble sort");
Label outputBubble = new Label();
Text inputPromptBubble = new Text("Enter a list of integers seperated by spaces:");
TextField inputTextBubble = new TextField();
Button backBtnBubble = new Button("Back");
backBtnBubble.setOnAction( e -> window.setScene(menuScene) );
Button submitBtnBubble = new Button("Submit");
submitBtnBubble.setOnAction(e -> {
if( validateArray(inputTextBubble.getText()) ) {
int[] arr = parseArray(inputTextBubble.getText());
double startTime = System.nanoTime();
int[] sortedArr = Algorithms.bubbleSort(arr);
double endTime = System.nanoTime();
double elapsedTime = (endTime - startTime)/1000;
outputBubble.setText("Sorted Array: " + Arrays.toString(sortedArr) +
"\nExecuted in " + elapsedTime + " microseconds. ");
} else {
outputBubble.setText("Invalid input. Please try again");
}
});
// Layout
VBox bubbleSortLayout = new VBox(10);
bubbleSortLayout.setPadding(new Insets(20,20,20,20));
bubbleSortLayout.getChildren().addAll(bubbleSortLabel, inputPromptBubble,
inputTextBubble, submitBtnBubble, outputBubble, backBtnBubble);
/*
Recursive bubble sort
*/
Label recurBubbleSortLabel = new Label("Recursive bubble sort");
Label outputRecurBubble = new Label();
Text inputPromptRecurBubble = new Text("Enter a list of integers seperated by spaces:");
TextField inputTextRecurBubble = new TextField();
Button backBtnRecurBubble = new Button("Back");
backBtnRecurBubble.setOnAction( e -> window.setScene(menuScene) );
Button submitBtnRecurBubble = new Button("Submit");
submitBtnRecurBubble.setOnAction(e -> {
if( validateArray(inputTextRecurBubble.getText()) ) {
int[] arr = parseArray(inputTextRecurBubble.getText());
double startTime = System.nanoTime();
int[] sortedArr = Algorithms.recursiveBubbleSort(arr, arr.length);
double endTime = System.nanoTime();
double elapsedTime = (endTime - startTime)/1000;
outputRecurBubble.setText("Sorted Array: " + Arrays.toString(sortedArr) +
"\nExecuted in " + elapsedTime + " microseconds. ");
} else {
outputRecurBubble.setText("Invalid input. Please try again");
}
});
// Layout
VBox recurBubbleSortLayout = new VBox(10);
recurBubbleSortLayout.setPadding(new Insets(20,20,20,20));
recurBubbleSortLayout.getChildren().addAll(recurBubbleSortLabel, inputPromptRecurBubble,
inputTextRecurBubble, submitBtnRecurBubble, outputRecurBubble, backBtnRecurBubble);
// ---- Searching Algorithms ----
/*
Linear search layout
*/
Label labelLS = new Label("Linear search");
Label outputLS = new Label();
Text inputPromptLS = new Text("Enter a list of integers seperated by spaces:");
TextField inputTextLS = new TextField();
Button submitBtnLS = new Button("Submit");
AlertBox alertBoxLS = new AlertBox();
Button backBtnLS = new Button("Back");
backBtnLS.setOnAction( e -> window.setScene(menuScene) );
submitBtnLS.setOnAction( e -> {
if( validateArray(inputTextLS.getText()) ) {
alertBoxLS.display("Algorithms", "Enter a target element");
TextField targetTextLS = alertBoxLS.getTarget();
int target = Integer.parseInt(targetTextLS.getText());
int[] arr = parseArray(inputTextLS.getText());
double startTime = System.nanoTime();
int index = Algorithms.linearSearch(arr, target, 0, arr.length);
double endTime = System.nanoTime();
double elapsedTime = (endTime - startTime)/1000;
if(index != -1)
outputLS.setText("Target '" + targetTextLS.getText() + "' found at index: " + index
+ " in " + elapsedTime + " microseconds.");
else
outputLS.setText("Target not found.");
} else {
outputLS.setText("Invalid input. Please try again");
}
});
VBox linearSearchLayout = new VBox(10);
linearSearchLayout.setPadding(new Insets(20,20,20,20));
linearSearchLayout.getChildren().addAll(labelLS, inputPromptLS, inputTextLS,
submitBtnLS, outputLS, backBtnLS);
/*
Binary search layout
*/
Label labelBS = new Label("Binary search");
Label outputBS = new Label();
Text inputPromptBS = new Text("Enter a list of integers seperated by spaces:");
TextField inputTextBS = new TextField();
Button submitBtnBS = new Button("Submit");
AlertBox alertBoxBS = new AlertBox();
Button backBtnBS = new Button("Back");
backBtnBS.setOnAction( e -> window.setScene(menuScene) );
submitBtnBS.setOnAction(e -> {
if( validateArray(inputTextBS.getText()) ) {
alertBoxBS.display("Algorithms", "Enter a target element");
TextField targetTextBS = alertBoxBS.getTarget();
int target = Integer.parseInt(targetTextBS.getText());
int[] arr = parseArray(inputTextBS.getText());
arr = Algorithms.selectionSort(arr);
double startTime = System.nanoTime();
int index = Algorithms.binarySearch(arr, target, 0, arr.length-1);
double endTime = System.nanoTime();
double elapsedTime = (endTime - startTime)/1000;
if(index != -1)
outputBS.setText("Sorted Array: " + Arrays.toString(arr) + "\nTarget '" + targetTextBS.getText() + "' found at index: " + index
+ " in " + elapsedTime + " microseconds.");
else
outputBS.setText("Target not found.");
} else {
outputBS.setText("Invalid input. Please try again");
}
});
VBox binarySearchLayout = new VBox(10);
binarySearchLayout.setPadding(new Insets(20,20,20,20));
binarySearchLayout.getChildren().addAll(labelBS, inputPromptBS, inputTextBS,
submitBtnBS, outputBS, backBtnBS);
/*
Jump search layout
*/
Label labelJump = new Label("Jump search");
Label outputJump = new Label();
Text inputPromptJump = new Text("Enter a list of integers seperated by spaces:");
TextField inputTextJump = new TextField();
Button submitBtnJump = new Button("Submit");
AlertBox alertBoxJump = new AlertBox();
Button backBtnJump = new Button("Back");
backBtnJump.setOnAction( e -> window.setScene(menuScene) );
submitBtnJump.setOnAction(e -> {
if( validateArray(inputTextJump.getText()) ) {
alertBoxJump.display("Algorithms", "Enter a target element");
TextField targetTextJump = alertBoxJump.getTarget();
int target = Integer.parseInt(targetTextJump.getText());
int[] arr = parseArray(inputTextJump.getText());
arr = Algorithms.mergeSort(arr, 0, arr.length-1);
double startTime = System.nanoTime();
int index = Algorithms.jumpSearch(arr, target);
double endTime = System.nanoTime();
double elapsedTime = (endTime - startTime)/1000;
if(index != -1)
outputJump.setText("Sorted Array: " + Arrays.toString(arr) + "\nTarget '" + targetTextJump.getText() + "' found at index: " + index
+ " in " + elapsedTime + " microseconds.");
else
outputJump.setText("Target not found.");
} else {
outputJump.setText("Invalid input. Please try again");
}
});
VBox jumpSearchLayout = new VBox(10);
jumpSearchLayout.setPadding(new Insets(20,20,20,20));
jumpSearchLayout.getChildren().addAll(labelJump, inputPromptJump, inputTextJump,
submitBtnJump, outputJump, backBtnJump);
/*
Interpolation search
*/
Label labelIS = new Label("Binary search");
Label outputIS = new Label();
Text inputPromptIS = new Text("Enter a list of integers seperated by spaces:");
TextField inputTextIS = new TextField();
Button submitBtnIS = new Button("Submit");
AlertBox alertBoxIS = new AlertBox();
Button backBtnIS = new Button("Back");
backBtnIS.setOnAction( e -> window.setScene(menuScene) );
submitBtnIS.setOnAction(e -> {
if( validateArray(inputTextIS.getText()) ) {
alertBoxIS.display("Algorithms", "Enter a target element");
TextField targetTextIS = alertBoxIS.getTarget();
int target = Integer.parseInt(targetTextIS.getText());
int[] arr = parseArray(inputTextIS.getText());
arr = Algorithms.selectionSort(arr);
double startTime = System.nanoTime();
int index = Algorithms.interpolationSearch(arr, target);
double endTime = System.nanoTime();
double elapsedTime = (endTime - startTime)/1000;
if(index != -1)
outputIS.setText("Sorted Array: " + Arrays.toString(arr) + "\nTarget '" + targetTextIS.getText() + "' found at index: " + index
+ " in " + elapsedTime + " microseconds.");
else
outputIS.setText("Target not found.");
} else {
outputIS.setText("Invalid input. Please try again");
}
});
VBox interSearchLayout = new VBox(10);
interSearchLayout.setPadding(new Insets(20,20,20,20));
interSearchLayout.getChildren().addAll(labelIS, inputPromptIS, inputTextIS,
submitBtnIS, outputIS, backBtnIS);
// Scenes
menuScene = new Scene(layout, WIDTH, HEIGHT);
selectSortScene = new Scene(selectSortLayout, WIDTH, HEIGHT);
mergeSortScene = new Scene(mergeSortLayout, WIDTH, HEIGHT);
bubbleSortScene = new Scene(bubbleSortLayout, WIDTH, HEIGHT);
linearSearchScene = new Scene(linearSearchLayout, WIDTH, HEIGHT);
binarySearchScene = new Scene(binarySearchLayout, WIDTH, HEIGHT);
jumpSearchScene = new Scene(jumpSearchLayout, WIDTH, HEIGHT);
recursiveBubbleSortScene = new Scene(recurBubbleSortLayout, WIDTH, HEIGHT);
interSearchScene = new Scene(interSearchLayout, WIDTH, HEIGHT);
window.setTitle("Algorithms");
window.setScene(menuScene);
window.show();
}
private boolean validateArray(String input) {
boolean errorCaught = false;
String[] tempArray;
String delimiter = " ";
tempArray = input.split(delimiter);
int[] resultArray = new int[tempArray.length];
/* print substrings */
for (int i = 0; i < tempArray.length; i++) {
try {
resultArray[i] = Integer.parseInt(tempArray[i]);
} catch(NumberFormatException e) {
errorCaught = true;
}
}
return !errorCaught;
}
private int[] parseArray(String input) {
String[] tempArray;
String delimiter = " ";
tempArray = input.split(delimiter);
int[] resultArray = new int[tempArray.length];
/* print substrings */
for (int i = 0; i < tempArray.length; i++)
resultArray[i] = Integer.parseInt(tempArray[i]);
return resultArray;
}
}