-
Notifications
You must be signed in to change notification settings - Fork 0
/
searching.c
104 lines (90 loc) · 2.53 KB
/
searching.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <stdio.h>
int binaryIterative(int arr[], int size, int target) {
int left = 0;
int right = size - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == target) {
return mid;
} else if (arr[mid] < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return -1;
}
int binaryRecursive(int arr[], int left, int right, int target) {
if (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == target) {
return mid;
} else if (arr[mid] < target) {
return binaryRecursive(arr, mid + 1, right, target);
} else {
return binaryRecursive(arr, left, mid - 1, target);
}
}
return -1;
}
int linearSearch(int arr[], int size, int target) {
for (int i = 0; i < size; i++) {
if (arr[i] == target) {
return i;
}
}
return -1;
}
int linearRecursive(int arr[], int start, int end, int target) {
if (start > end) {
return -1;
}
if (arr[start] == target) {
return start;
}
return linearRecursive(arr, start + 1, end, target);
}
int main() {
int choice, target;
int size;
printf("Enter the size of the array: ");
scanf("%d", &size);
int arr[size];
printf("Enter the elements of the array:\n");
for (int i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}
printf("Choose a searching technique:\n");
printf("1. Binary Iterative\n");
printf("2. Binary Recursive\n");
printf("3. Linear Iterative\n");
printf("4. Linear Recursive\n");
printf("Enter your choice (1/2/3/4): ");
scanf("%d", &choice);
printf("Enter the target value: ");
scanf("%d", &target);
int result = -1;
switch (choice) {
case 1:
result = binaryIterative(arr, size, target);
break;
case 2:
result = binaryRecursive(arr, 0, size - 1, target);
break;
case 3:
result = linearSearch(arr, size, target);
break;
case 4:
result = linearRecursive(arr, 0, size - 1, target);
break;
default:
printf("Invalid choice.\n");
break;
}
if (result != -1) {
printf("Element found at index: %d\n", result);
} else {
printf("Element not found.\n");
}
return 0;
}