Repository navigation
Expand file tree
/
Copy pathSearching.java
More file actions
189 lines (156 loc) · 7.46 KB
/
Copy pathSearching.java
File metadata and controls
189 lines (156 loc) · 7.46 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
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Searching extends JFrame {
private JTextField inputField;
private JTextArea resultArea;
public Searching() {
setTitle("Searching Algorithms");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setSize(400, 300);
setLocationRelativeTo(null);
UIManager.put("OptionPane.messageForeground", Color.GREEN);
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
JPanel inputPanel = new JPanel(new FlowLayout());
inputField = new JTextField(10);
JButton clearButton = new JButton("Clear");
clearButton.addActionListener(new ClearButtonListener());
inputPanel.add(new JLabel("Enter Elements: "));
inputPanel.add(inputField);
inputPanel.add(clearButton);
resultArea = new JTextArea();
resultArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(resultArea);
JPanel searchOptionsPanel = new JPanel();
searchOptionsPanel.setLayout(new BoxLayout(searchOptionsPanel, BoxLayout.Y_AXIS));
JButton linearSearchButton = new JButton("Linear Search");
linearSearchButton.addActionListener(new LinearSearchButtonListener());
JButton binarySearchButton = new JButton("Binary Search");
binarySearchButton.addActionListener(new BinarySearchButtonListener());
searchOptionsPanel.add(linearSearchButton);
searchOptionsPanel.add(Box.createVerticalStrut(10));
searchOptionsPanel.add(binarySearchButton);
panel.add(inputPanel, BorderLayout.NORTH);
panel.add(scrollPane, BorderLayout.CENTER);
panel.add(searchOptionsPanel, BorderLayout.EAST);
add(panel);
}
private class ClearButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
inputField.setText("");
resultArea.setText("");
}
}
private class LinearSearchButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
resultArea.setText("");
String input = inputField.getText().trim();
try {
String[] elements = input.split("\\s+");
int[] array = new int[elements.length];
for (int i = 0; i < elements.length; i++) {
array[i] = Integer.parseInt(elements[i]);
}
int key = Integer.parseInt(JOptionPane.showInputDialog(null,
"Enter the element to search:", "Linear Search", JOptionPane.PLAIN_MESSAGE));
resultArea.append("Linear Search:\n");
resultArea.append("Linear search is a simple searching algorithm that sequentially checks each element of the array until it finds the target element.\n\n");
int index = linearSearch(array, key);
if (index != -1) {
JOptionPane.showMessageDialog(null, "Element " + key + " found at index " + index,
"Search Result", JOptionPane.INFORMATION_MESSAGE);
resultArea.append("Element " + key + " found at index " + index);
} else {
JOptionPane.showMessageDialog(null, "Element not found in the array",
"Search Result", JOptionPane.INFORMATION_MESSAGE);
resultArea.append("Element not found in the array");
}
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(null, "Invalid input! Please enter integers separated by spaces.",
"Invalid Input", JOptionPane.ERROR_MESSAGE);
}
}
}
private class BinarySearchButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
resultArea.setText("");
String input = inputField.getText().trim();
try {
String[] elements = input.split("\\s+");
int[] array = new int[elements.length];
for (int i = 0; i < elements.length; i++) {
array[i] = Integer.parseInt(elements[i]);
}
if (!isSorted(array)) {
JOptionPane.showMessageDialog(null, "Array must be sorted for Binary Search!",
"Sorting Required", JOptionPane.ERROR_MESSAGE);
return;
}
int key = Integer.parseInt(JOptionPane.showInputDialog(null,
"Enter the element to search:", "Binary Search", JOptionPane.PLAIN_MESSAGE));
resultArea.append("Binary Search:\n");
resultArea.append("Binary search is an efficient searching algorithm that works on sorted arrays. It repeatedly divides the search range in half by comparing the middle element with the target element.\n\n");
int index = binarySearch(array, key);
if (index != -1) {
JOptionPane.showMessageDialog(null, "Element " + key + " found at index " + index,
"Search Result", JOptionPane.INFORMATION_MESSAGE);
resultArea.append("Element " + key + " found at index " + index);
} else {
JOptionPane.showMessageDialog(null, "Element not found in the array",
"Search Result", JOptionPane.INFORMATION_MESSAGE);
resultArea.append("Element not found in the array");
}
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(null, "Invalid input! Please enter integers separated by spaces.",
"Invalid Input", JOptionPane.ERROR_MESSAGE);
}
}
}
private int linearSearch(int[] array, int key) {
for (int i = 0; i < array.length; i++) {
resultArea.append("Comparing " + key + " with " + array[i] + "\n");
if (array[i] == key) {
return i;
}
}
return -1;
}
private int binarySearch(int[] array, int key) {
int low = 0;
int high = array.length - 1;
int step = 1; // Step counter
while (low <= high) {
int mid = (low + high) / 2;
resultArea.append("Step " + step + ": ");
resultArea.append("Search range: [" + low + " - " + high + "]\n");
resultArea.append("Comparing " + key + " with " + array[mid] + "\n");
step++;
if (array[mid] == key) {
return mid;
} else if (array[mid] < key) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return -1;
}
private boolean isSorted(int[] array) {
for (int i = 1; i < array.length; i++) {
if (array[i] < array[i - 1]) {
return false;
}
}
return true;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Searching searching = new Searching();
searching.setVisible(true);
}
});
}
}