-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlgorithmAnalyzer.java
More file actions
259 lines (217 loc) · 7.88 KB
/
Copy pathAlgorithmAnalyzer.java
File metadata and controls
259 lines (217 loc) · 7.88 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
import java.util.Arrays;
import java.util.Scanner;
public class AlgorithmAnalyzer {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Algorithm Analyzer");
System.out.println("1. Searching Algorithms");
System.out.println("2. Sorting Algorithms");
System.out.print("Enter your choice (1 or 2): ");
int choice = scanner.nextInt();
scanner.nextLine(); // consume newline
System.out.print("Enter the list of items (comma separated): ");
String[] itemsStr = scanner.nextLine().split(",");
int[] items = new int[itemsStr.length];
for (int i = 0; i < itemsStr.length; i++) {
items[i] = Integer.parseInt(itemsStr[i].trim());
}
if (choice == 1) {
// Searching
System.out.print("Enter the value to search: ");
int searchValue = scanner.nextInt();
System.out.println("\nSelect a searching algorithm:");
System.out.println("1. Linear Search (O(n))");
System.out.println("2. Binary Search (O(log n)) - Array must be sorted");
int searchChoice = scanner.nextInt();
int result = -1;
long startTime = System.nanoTime();
if (searchChoice == 1) {
result = linearSearch(items, searchValue);
} else if (searchChoice == 2) {
// Binary search requires sorted array
Arrays.sort(items);
System.out.println("Array sorted for binary search: " + Arrays.toString(items));
result = binarySearch(items, searchValue);
}
long endTime = System.nanoTime();
long duration = endTime - startTime;
System.out.println("\nResults:");
if (result != -1) {
System.out.println("Value found at index: " + result);
} else {
System.out.println("Value not found in the array");
}
System.out.println("Execution time: " + duration + " nanoseconds");
} else if (choice == 2) {
// Sorting
System.out.println("\nSelect a sorting algorithm:");
System.out.println("1. Bubble Sort (O(n^2))");
System.out.println("2. Selection Sort (O(n^2))");
System.out.println("3. Insertion Sort (O(n^2))");
System.out.println("4. Merge Sort (O(n log n))");
System.out.println("5. Quick Sort (O(n log n) average)");
int sortChoice = scanner.nextInt();
int[] sortedArray = items.clone();
long startTime = System.nanoTime();
switch (sortChoice) {
case 1:
bubbleSort(sortedArray);
break;
case 2:
selectionSort(sortedArray);
break;
case 3:
insertionSort(sortedArray);
break;
case 4:
mergeSort(sortedArray, 0, sortedArray.length - 1);
break;
case 5:
quickSort(sortedArray, 0, sortedArray.length - 1);
break;
default:
System.out.println("Invalid choice");
return;
}
long endTime = System.nanoTime();
long duration = endTime - startTime;
System.out.println("\nResults:");
System.out.println("Original array: " + Arrays.toString(items));
System.out.println("Sorted array: " + Arrays.toString(sortedArray));
System.out.println("Execution time: " + duration + " nanoseconds");
}
scanner.close();
}
// Searching Algorithms
public static int linearSearch(int[] array, int value) {
for (int i = 0; i < array.length; i++) {
if (array[i] == value) {
return i;
}
}
return -1;
}
public static int binarySearch(int[] array, int value) {
int left = 0;
int right = array.length - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (array[mid] == value) {
return mid;
}
if (array[mid] < value) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return -1;
}
// Sorting Algorithms
public static void bubbleSort(int[] array) {
int n = array.length;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (array[j] > array[j + 1]) {
// swap
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
}
public static void selectionSort(int[] array) {
int n = array.length;
for (int i = 0; i < n - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < n; j++) {
if (array[j] < array[minIndex]) {
minIndex = j;
}
}
// swap
int temp = array[minIndex];
array[minIndex] = array[i];
array[i] = temp;
}
}
public static void insertionSort(int[] array) {
int n = array.length;
for (int i = 1; i < n; i++) {
int key = array[i];
int j = i - 1;
while (j >= 0 && array[j] > key) {
array[j + 1] = array[j];
j--;
}
array[j + 1] = key;
}
}
public static void mergeSort(int[] array, int left, int right) {
if (left < right) {
int mid = left + (right - left) / 2;
mergeSort(array, left, mid);
mergeSort(array, mid + 1, right);
merge(array, left, mid, right);
}
}
private static void merge(int[] array, int left, int mid, int right) {
int n1 = mid - left + 1;
int n2 = right - mid;
int[] L = new int[n1];
int[] R = new int[n2];
for (int i = 0; i < n1; i++) {
L[i] = array[left + i];
}
for (int j = 0; j < n2; j++) {
R[j] = array[mid + 1 + j];
}
int i = 0, j = 0, k = left;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
array[k] = L[i];
i++;
} else {
array[k] = R[j];
j++;
}
k++;
}
while (i < n1) {
array[k] = L[i];
i++;
k++;
}
while (j < n2) {
array[k] = R[j];
j++;
k++;
}
}
public static void quickSort(int[] array, int low, int high) {
if (low < high) {
int pi = partition(array, low, high);
quickSort(array, low, pi - 1);
quickSort(array, pi + 1, high);
}
}
private static int partition(int[] array, int low, int high) {
int pivot = array[high];
int i = low - 1;
for (int j = low; j < high; j++) {
if (array[j] < pivot) {
i++;
// swap
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
// swap
int temp = array[i + 1];
array[i + 1] = array[high];
array[high] = temp;
return i + 1;
}
}