You are on page 1of 1

#include<stdio.

h>
#include<dos.h>
#include<stdlib.h>
int ls(int a[], int n int key){
delay(100);
if(n<0)
return -1;
if(a[n]==key)
return key;
return ls(a,n-1,key);
}
int bs(int a[], int low, int high, int key){
delay(100);
int mid=(low+high)/2;
if(low>high)
return -1;
if(a[mid]==key)
return mid;
if(a[mid]<key)
return bs(a,mid+1,high,key);
return bs(a,low,mid-1,key);
}
void main() {
int a[100],n,key,i,r;
clock_t s,e;
printf("enter number of elements:");
scanf("%d",&n);
Printf("linear Search\n");
for(i=0;i<n;i++)
a[i]=i+1;
printf("enter number to be searched:");
scanf("%d",&key);
s=clock();
ls(a,n-1,key);
e=clock();
if(r!=-1)
printf("found at %d ",r);
else
printf("not found ");
printf("in %f",(e-s)/CKL_TKC);
Printf("\n\nBinary Search\n");
s=clock();
bs(a,n-1,key);
e=clock();
if(r!=-1)
printf("found at %d ",r);
else
printf("not found ");
printf("in %f",(e-s)/CKL_TKC);
}

You might also like