-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprogram2.c
80 lines (80 loc) · 1.46 KB
/
program2.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
int binary(int array[],int key,int n){
int low,high,mid;
low=0;
high=n-1;
while(low<high){
mid=(low+high)/2;
if(key==array[mid]){
return mid;
}
else if(key<array[mid])
high=mid-1;
else
low=mid+1;
}
return -1;
}
int linary(int array[],int key,int n){
int i;
for(i=0;i<n;i++){
if(array[i]==key)
return 1;
else
return -1;
}
}
void delay(){
int j;
for(j=0;j<70000000;j++);
}
void main(){
int n,array[5000],key,ch;
unsigned long int i,flag;
clock_t s,e;
while(1){
printf("\nEnter your search choice:\n1. Binary search 2. Linear search\n");
scanf("%d",&ch);
switch(ch){
case 1: n=1000;
while(n<=3000){
for(i=0;i<n;i++){
array[i];
}
key=array[n-1];
s=clock();
flag=binary(array,key,n);
if(flag>=1)
printf("\n Element found");
else
printf("Element not found");
delay();
e=clock();
printf("\n Time taken for %d elements is %f\n",n,((double)(e-s))/CLOCKS_PER_SEC);
n=n+1000;
}
break;
case 2: n=1000;
while(n<=3000){
for(i=0;i<n;i++){
array[i];
}
key=array[n-1];
s=clock();
flag=linary(array,key,n);
if(flag=1)
printf("\nElement found");
else
printf("Element not found");
delay();
e=clock();
printf("\n Time taken for %d elements is %f\n",n,((double)(e-s))/CLOCKS_PER_SEC);
n=n+1000;
}
break;
default:exit(0);
}
}
}