-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinary with rec
48 lines (46 loc) · 898 Bytes
/
binary with rec
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
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void delay()
{
int i,j,temp;
for(i=0;i<500000;i++)
temp=30/333;
return;
}
int binary(int l,int h,int arr[],int key)
{
int m;
delay();
m=(l+h)/2;
if(l>h)
return -1;
if(arr[m]==key)
return (m+1);
else if(key>arr[m])
return (binary(m+1,h,arr,key));
else
return (binary(l,m-1,arr,key));
}
int main()
{
clock_t start,end;
int m,l,h,flag;
int n, arr[10000],key,i;
printf("Enter the value of n: ");
scanf("%d",&n);
for(i=0;i<n;i++)
arr[i]=i;
key=arr[n-1];
l=0;
h=n-1;
i=0;
start=clock();
flag=binary(l,h,arr,key);
if(flag==-1)
printf("\nKey not found!");
else
printf("\nKey found at %d position",flag);
end=clock();
printf("\nTime taken: %f",(double)(end-start)/CLOCKS_PER_SEC);
}