Repository navigation
Expand file tree
/
Copy pathSortingAlgos.java
More file actions
285 lines (218 loc) · 9.66 KB
/
Copy pathSortingAlgos.java
File metadata and controls
285 lines (218 loc) · 9.66 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
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
public class SortingAlgos extends JFrame {
private JTextField inputField;
private JTextArea stepsArea;
public SortingAlgos() {
setTitle("Sorting Algorithms");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setLocationRelativeTo(null);
setResizable(true);
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
panel.setBackground(Color.WHITE);
JButton bubbleSortButton = createButton("Bubble Sort", Color.CYAN, new BubbleSortButtonListener());
JButton insertionSortButton = createButton("Insertion Sort", Color.ORANGE, new InsertionSortButtonListener());
JButton selectionSortButton = createButton("Selection Sort", Color.GREEN, new SelectionSortButtonListener());
JLabel inputLabel = new JLabel("Input:");
inputField = new JTextField(20);
JButton sortButton = createButton("Sort", Color.YELLOW, new SortButtonListener());
JButton clearButton = createButton("Clear", Color.LIGHT_GRAY, new ClearButtonListener());
stepsArea = new JTextArea(15, 40);
stepsArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(stepsArea);
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 1.0;
gbc.insets = new Insets(5, 5, 5, 5);
gbc.gridx = 0;
gbc.gridy = 0;
panel.add(bubbleSortButton, gbc);
gbc.gridx = 1;
gbc.gridy = 0;
panel.add(insertionSortButton, gbc);
gbc.gridx = 2;
gbc.gridy = 0;
panel.add(selectionSortButton, gbc);
gbc.gridx = 0;
gbc.gridy = 1;
panel.add(inputLabel, gbc);
gbc.gridx = 1;
gbc.gridy = 1;
gbc.gridwidth = 2;
panel.add(inputField, gbc);
gbc.gridx = 3;
gbc.gridy = 1;
gbc.gridwidth = 1;
panel.add(sortButton, gbc);
gbc.gridx = 4;
gbc.gridy = 1;
panel.add(clearButton, gbc);
gbc.gridx = 0;
gbc.gridy = 2;
gbc.gridwidth = 5;
panel.add(scrollPane, gbc);
setContentPane(panel);
pack();
}
private JButton createButton(String text, Color color, ActionListener listener) {
JButton button = new JButton(text);
button.setBackground(color);
button.addActionListener(listener);
return button;
}
private class BubbleSortButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String inputText = inputField.getText().trim();
int[] array = parseArray(inputText);
if (array != null) {
bubbleSort(array);
}
}
}
private class InsertionSortButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String inputText = inputField.getText().trim();
int[] array = parseArray(inputText);
if (array != null) {
insertionSort(array);
}
}
}
private class SelectionSortButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String inputText = inputField.getText().trim();
int[] array = parseArray(inputText);
if (array != null) {
selectionSort(array);
}
}
}
private class SortButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String inputText = inputField.getText().trim();
int[] array = parseArray(inputText);
if (array != null) {
bubbleSort(array);
insertionSort(array);
selectionSort(array);
}
}
}
private class ClearButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
inputField.setText("");
stepsArea.setText("");
}
}
private int[] parseArray(String inputText) {
if (inputText.isEmpty()) {
showErrorMessage("Please enter an array.");
return null;
}
String[] values = inputText.split("\\s+");
int[] array = new int[values.length];
try {
for (int i = 0; i < values.length; i++) {
array[i] = Integer.parseInt(values[i]);
}
} catch (NumberFormatException ex) {
showErrorMessage("Invalid array format. Please enter integers separated by spaces.");
return null;
}
return array;
}
private void bubbleSort(int[] array) {
int[] sortedArray = Arrays.copyOf(array, array.length);
int n = sortedArray.length;
StringBuilder sb = new StringBuilder();
sb.append("Bubble Sort Steps:\n");
for (int i = 0; i < n - 1; i++) {
boolean swapped = false;
for (int j = 0; j < n - i - 1; j++) {
// Compare adjacent elements
sb.append(" - Comparing elements: ").append(sortedArray[j]).append(" and ").append(sortedArray[j + 1]).append("\n");
if (sortedArray[j] > sortedArray[j + 1]) {
// Swap elements if they are in the wrong order
int temp = sortedArray[j];
sortedArray[j] = sortedArray[j + 1];
sortedArray[j + 1] = temp;
sb.append(" - Swapped ").append(sortedArray[j]).append(" with ").append(sortedArray[j + 1]).append("\n");
swapped = true;
}
}
if (!swapped) {
sb.append("No more swaps required, the array is already sorted.\n");
break;
}
sb.append(" - Current Array: ").append(Arrays.toString(sortedArray)).append("\n");
}
showResult("Bubble Sort Result:", sortedArray, sb.toString());
}
private void insertionSort(int[] array) {
int[] sortedArray = Arrays.copyOf(array, array.length);
int n = sortedArray.length;
StringBuilder sb = new StringBuilder();
sb.append("Insertion Sort Steps:\n");
for (int i = 1; i < n; ++i) {
int key = sortedArray[i];
int j = i - 1;
sb.append(" - Element ").append(key).append(" at index ").append(i).append(" is selected for insertion.\n");
while (j >= 0 && sortedArray[j] > key) {
sortedArray[j + 1] = sortedArray[j];
j = j - 1;
sb.append(" - Shifting element ").append(sortedArray[j + 1]).append(" at index ").append(j + 1).append(" to the right.\n");
}
sortedArray[j + 1] = key;
sb.append(" - Element ").append(key).append(" is inserted at index ").append(j + 1).append(".\n");
sb.append(" - Current Array: ").append(Arrays.toString(sortedArray)).append("\n");
}
showResult("Insertion Sort Result:", sortedArray, sb.toString());
}
private void selectionSort(int[] array) {
int[] sortedArray = Arrays.copyOf(array, array.length);
int n = sortedArray.length;
StringBuilder sb = new StringBuilder();
sb.append("Selection Sort Steps:\n");
for (int i = 0; i < n - 1; i++) {
int minIndex = i;
sb.append(" - Selected minimum element: ").append(sortedArray[minIndex]).append(" at index ").append(minIndex).append("\n");
for (int j = i + 1; j < n; j++) {
if (sortedArray[j] < sortedArray[minIndex]) {
minIndex = j;
}
}
if (minIndex != i) {
int temp = sortedArray[minIndex];
sortedArray[minIndex] = sortedArray[i];
sortedArray[i] = temp;
sb.append(" - Swapped element ").append(sortedArray[i]).append(" at index ").append(i)
.append(" with minimum element ").append(sortedArray[minIndex]).append(" at index ").append(minIndex)
.append(" within the range [").append(i).append(", ").append(n - 1).append("]\n");
} else {
sb.append(" - Minimum element ").append(sortedArray[minIndex]).append(" at index ").append(minIndex)
.append(" is already in the correct position within the range [").append(i).append(", ").append(n - 1).append("]\n");
}
sb.append(" - Current Array: ").append(Arrays.toString(sortedArray)).append("\n");
}
showResult("Selection Sort Result:", sortedArray, sb.toString());
}
private void showResult(String title, int[] sortedArray, String steps) {
JOptionPane.showMessageDialog(SortingAlgos.this, Arrays.toString(sortedArray), title, JOptionPane.INFORMATION_MESSAGE);
stepsArea.setText(steps);
}
private void showErrorMessage(String message) {
JOptionPane.showMessageDialog(SortingAlgos.this, message, "Error", JOptionPane.ERROR_MESSAGE);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
SortingAlgos sortingAlgos = new SortingAlgos();
sortingAlgos.setVisible(true);
}
});
}
}