forked from puruagarwal1/C_dir
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_search.c
More file actions
39 lines (36 loc) · 817 Bytes
/
binary_search.c
File metadata and controls
39 lines (36 loc) · 817 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
32
33
34
35
36
37
38
39
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<time.h>
#include<stdlib.h>
int binary_search(int arr[],int element,int size){
int low,mid,high;
low=0;
high = size-1;
mid=low+high/2;
while(low<=high){
if(arr[mid]==element){
return mid;
}
if(arr[mid]<element){
low=mid+1;
}
else{
high=mid-1;
}
}
}
int main(){
int size=6,element;
int arr[100];
for (int i = 0; i < size; i++){
printf("enter element %d ",i+1);
scanf("%d",&arr[i]);
}
printf("enter an element to search");
scanf("%d",&element);
int found;
found=binary_search(arr,element,6);
printf("element found at index %d",found+1);
return 0;
}