-
Notifications
You must be signed in to change notification settings - Fork 0
/
searchingalgorithm1.c
84 lines (76 loc) · 2.5 KB
/
searchingalgorithm1.c
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
#include <stdio.h>
int main() {
int arr[100], choice, key, n;
// Input the number of elements
printf("Enter number of elements (max 100): ");
scanf("%d", &n);
// Input the elements of the array
printf("Enter %d elements:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
// Menu system without using do-while loop
while (1) {
// Display the menu
printf("\nMenu:\n");
printf("1. Linear Search\n");
printf("2. Binary Search\n");
printf("3. Exit\n");
printf("Choose an option: ");
scanf("%d", &choice);
if (choice == 1) {
// Linear Search
printf("Enter the element to search: ");
scanf("%d", &key);
int found = 0;
for (int i = 0; i < n; i++) {
if (arr[i] == key) {
printf("Element %d found at index %d (Linear Search)\n", key, i);
found = 1;
break;
}
}
if (!found) {
printf("Element %d not found (Linear Search)\n", key);
}
} else if (choice == 2) {
// Binary Search (first, sort the array)
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
// Perform Binary Search
printf("Enter the element to search: ");
scanf("%d", &key);
int beg = 0, end = n - 1, found = 0;
while (beg <= end) {
int mid = beg + (end - beg) / 2;
if (arr[mid] == key) {
printf("Element %d found at index %d (Binary Search)\n", key, mid);
found = 1;
break;
} else if (arr[mid] < key) {
beg = mid + 1;
} else {
end = mid - 1;
}
}
if (!found) {
printf("Element %d not found (Binary Search)\n", key);
}
} else if (choice == 3) {
// Exit the program
printf("Exiting...\n");
break;
} else {
// Invalid option
printf("Invalid choice! Please try again.\n");
}
}
return 0;
}