-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortingUI.java
More file actions
126 lines (106 loc) · 4.24 KB
/
Copy pathSortingUI.java
File metadata and controls
126 lines (106 loc) · 4.24 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
package project;
import java.awt.*;
import java.awt.event.*;
class Sorting extends SortingAlgorithm {
void displayarray(int[] arr, TextArea output) {
StringBuilder sb = new StringBuilder();
for (int val : arr) {
sb.append(val).append(" ");
}
output.append(sb.toString().trim() + "\n");
}
}
public class SortingUI extends Frame {
private TextField inputField;
private TextArea outputArea;
private Button bubbleButton, mergeButton, quickButton, selectionButton, insertionButton, clearButton;
private Sorting sorter;
public SortingUI() {
sorter = new Sorting();
setTitle("Sorting Algorithms");
setSize(550, 350);
setLayout(new BorderLayout(10, 10));
setBackground(Color.LIGHT_GRAY);
Panel inputPanel = new Panel(new BorderLayout());
Label inputLabel = new Label("Enter numbers (comma-separated):");
inputField = new TextField("", 40);
inputPanel.add(inputLabel, BorderLayout.NORTH);
inputPanel.add(inputField, BorderLayout.CENTER);
add(inputPanel, BorderLayout.NORTH);
outputArea = new TextArea(8, 50);
outputArea.setEditable(false);
add(outputArea, BorderLayout.CENTER);
Panel buttonPanel = new Panel(new GridLayout(6, 1, 5, 5));
bubbleButton = new Button("Bubble Sort");
mergeButton = new Button("Merge Sort");
quickButton = new Button("Quick Sort");
selectionButton = new Button("Selection Sort");
insertionButton = new Button("Insertion Sort");
clearButton = new Button("Clear");
bubbleButton.addActionListener(e -> sortAndDisplay("Bubble Sort"));
mergeButton.addActionListener(e -> sortAndDisplay("Merge Sort"));
quickButton.addActionListener(e -> sortAndDisplay("Quick Sort"));
selectionButton.addActionListener(e -> sortAndDisplay("Selection Sort"));
insertionButton.addActionListener(e -> sortAndDisplay("Insertion Sort"));
clearButton.addActionListener(e -> {
inputField.setText("");
outputArea.setText("");
});
buttonPanel.add(bubbleButton);
buttonPanel.add(mergeButton);
buttonPanel.add(quickButton);
buttonPanel.add(selectionButton);
buttonPanel.add(insertionButton);
buttonPanel.add(clearButton);
add(buttonPanel, BorderLayout.SOUTH);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
dispose();
System.exit(0);
}
});
setResizable(false);
setVisible(true);
}
private void sortAndDisplay(String algorithm) {
try {
String input = inputField.getText().trim();
if (input.isEmpty()) {
outputArea.setText("Error: Please enter numbers.\n");
return;
}
String[] numbers = input.split(",");
int[] arr = new int[numbers.length];
for (int i = 0; i < numbers.length; i++) {
arr[i] = Integer.parseInt(numbers[i].trim());
}
int[] arrCopy = arr.clone();
switch (algorithm) {
case "Bubble Sort":
sorter.BubbleSort(arrCopy);
break;
case "Merge Sort":
sorter.MergeSort(arrCopy, 0, arrCopy.length - 1);
break;
case "Quick Sort":
sorter.PartitionSort(arrCopy, 0, arrCopy.length - 1);
break;
case "Selection Sort":
sorter.SelectionSort(arrCopy);
break;
case "Insertion Sort":
sorter.InsertionSort(arrCopy);
break;
}
outputArea.append("Sorted Array (" + algorithm + "):\n");
sorter.displayarray(arrCopy, outputArea);
} catch (NumberFormatException ex) {
outputArea.setText("Error: Please enter valid numbers.\n");
} catch (Exception ex) {
outputArea.setText("Error: An unexpected error occurred.\n");
}
}
public static void main(String[] args) {
new SortingUI();
}
}