-
Notifications
You must be signed in to change notification settings - Fork 0
/
FUNCTION-BINARYSEARCH.c
53 lines (42 loc) · 1.44 KB
/
FUNCTION-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
#include<stdio.h>
#include<conio.h>
int binaryserach(int arr[], int low, int high, int search)
{
int mid;
while(low<=high)
{
mid=(low+high)/2;
if(arr[mid]>search)
{
high=mid-1;
}
else if(arr[mid]<search)
{
low=mid+1;
}
else if(arr[mid]==search)
{
return mid;
}
}
return -1;
}
int main()
{
int arr[]={1,2,3,4,5,6,7,8,9,10};
int low=0;
int high=9;
int found;
int search=7;
int searchindex=binarysearch(arr,0,high,search);
if(searchindex==-1)
{
printf("unsuscefful");
}
else
{
printf("suscessfull :%d",searchindex);
}
getch();
return 0;
}