forked from Sourabh1Bhardwaj/Data-structure
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinarySearch.c
37 lines (37 loc) · 854 Bytes
/
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
#include<stdio.h>
int main()
{
int arr[50],n,search;
printf("Enter no. of element\n");
scanf("%d",&n);
printf("Enter the array element");
for(int i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
printf("Enter element those you want to search");
scanf("%d",&search);
int first = 0;
int last = n-1;
int middle =(first+last)/2;
while(first<=last)
{
if(arr[middle]<search)
{
first = middle + 1;
}
else if(arr[middle]==search)
{
printf("position:%d",middle+1);
break;
}
else{
last = middle - 1;
}
middle = (first+last)/2;
}
if(first>last){
printf("not found");
}
return 0;
}