forked from avastino7/Algorithms-Data-Structures
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbinarysearch
More file actions
31 lines (31 loc) · 785 Bytes
/
binarysearch
File metadata and controls
31 lines (31 loc) · 785 Bytes
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
#include<stdio.h>
#include<conio.h>
void main(){
int array[10],i,n,item,low,high,mid,flag;
printf("Enter the size of array");
scanf("%d",&n);
printf("Enter the elements of an array");
for(i=0;i<n;i++){
scanf("%d",&array[i]);
}
low=0; //low is lowest index value of an array
high=n-1;// high is the highest index value of an array
printf("Enter the element to search");
scanf("%d",&item);
while(low<=high){
mid=(low+high)/2;
if(item==array[mid]){
flag==1;
break;
}
else if(item>array[mid]){
low=mid+1;
}
else if(item<array[mid]){
high=mid-1;
}
}
if(flag==1){
printf("The element if found at the index%d",mid);
}
}