-
Notifications
You must be signed in to change notification settings - Fork 325
/
Copy pathBinarySearch.c
68 lines (50 loc) · 1.25 KB
/
BinarySearch.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
#include <stdio.h>
//Recursive Method
int binarySearchR(int arr[], int l, int r, int x)
{
if (r >= l) {
int mid = l + (r - l) / 2;
if (arr[mid] == x)
return mid;
if (arr[mid] > x)
return binarySearchR(arr, l, mid - 1, x);
return binarySearchR(arr, mid + 1, r, x);
}
return -1;
}
//Iterative Method
int binarySearchI(int arr[], int l, int r, int x)
{
while (l <= r) {
int m = l + (r - l) / 2;
if (arr[m] == x)
return m;
if (arr[m] < x)
l = m + 1;
else
r = m - 1;
}
return -1;
}
int main(void)
{
int n;
printf("Size : ");
scanf("%d",&n);
int arr[n];
for(int i=0;i<n;i++)
scanf("%d",&arr[i]);
int op;
printf("Enter choice : \n 1. Recursive Method \n 2. Iterative Method ");
scanf("%d",&op);
int x;
printf("Value to search : ");
scanf("%d",&x);
int result;
if(op==1)
result = binarySearchR(arr, 0, n - 1, x);
else
result = binarySearchI(arr, 0, n - 1, x);
(result == -1) ? printf("Element is not present in array") : printf("Element is present at index %d", result);
return 0;
}