-
Notifications
You must be signed in to change notification settings - Fork 367
Expand file tree
/
Copy pathbinary_search.c
More file actions
48 lines (43 loc) · 1.25 KB
/
binary_search.c
File metadata and controls
48 lines (43 loc) · 1.25 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
#include <stdio.h>
/*
<----------------------------------PROBLEM------------------------------------>
Given a sorted array arr[] of n elements, write a function to search a given
element x in arr[] and return the index of x in the array.
Variables used:
n = total number of elements in array.
arr = array initialized with the input size.
el = element to be searched in the array.
lt = left-most index of the search space.
rt = right-most index of the search space.
*/
int main()
{
int n;
scanf("%d", &n);
int arr[n];
for(int i=0; i<n; i++){
scanf("%d", &arr[i]);
}
int el;
scanf("%d", &el);
int lt=0, rt=n-1;
while(lt<=rt){
// in order to prevent overflow of `int`, it has been written lt + (rt-lt)/2 instead of (lt+rt)/2;
int mid = lt + (rt-lt)/2;
// element is equal to mid, return the index
if(arr[mid]==el){
printf("Element found:)\n");
return 0;
}
// element is larger than mid, then shift search space to right.
else if(arr[mid]<el){
lt=mid+1;
}
// element is smaller than mid, then shift search space to left.
else{
rt=mid-1;
}
}
printf("Element not found:(\n");
return 0;
}