-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinary Search.C
More file actions
37 lines (36 loc) · 740 Bytes
/
Binary Search.C
File metadata and controls
37 lines (36 loc) · 740 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
// Binary Search //
#include<stdio.h>
int main()
{
int A[12]={-2, -1, 0, 4, 6, 8, 9, 10, 45, 78, 89, 100},T,t;
scanf("%d",&T);
for(t=0;t<T;t=t+1)
{
int mid,i,n=12,left,right,k;
scanf("%d",&k);
left=0;
right=n-1;
while(left<=right)
{
mid=(left+right)/2;
if(A[mid]==k)
{
printf("%d found at index %d.\n",k,mid);
break;
}
else if(A[mid]<k)
{
left=mid+1;
}
else
{
right=mid-1;
}
}
if(left>right)
{
printf("%d not found.\n",k);
}
}
return(0);
}