-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2b.c
55 lines (55 loc) · 1.25 KB
/
2b.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
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#define ini 10
#define fin 100
#define ste 10
int c=0;
void writeCount(int n, FILE *fp)
{
fprintf(fp,"%d\t%d\n",n,c);
c=0;
}
int bSearch(int *a,int s,int e,int key)
{
int m= (s+e)/2;
c++;
if(a[m]==key) return m;
else
{
if(s>=e) return -1;
else if(key > a[m]) bSearch(a,m+1,e,key);
else bSearch(a,s,m-1,key);
}
}
void main()
{
int *a;
FILE *f=fopen("BSarrayInfo.txt","w");
FILE *fB=fopen("BSBest.txt","w");
FILE *fW=fopen("BSWorst.txt","w");
srand(time(0));
for(int n=ini;n<=fin;n+=ste)
{
a=(int *)malloc(n*sizeof(int));
fprintf(f,"Array : \n");
for(int i=0;i<n;i++)
{
a[i]=(rand()+1)%100;
fprintf(f,"%d\t",a[i]);
}
fprintf(f,"\nBest Search : %d\tx\tWorst Search : 9999\n",a[n/2]);
bSearch(a,0,n-1,a[(n-1)/2]);
writeCount(n,fB);
bSearch(a,0,n-1,9999);
writeCount(n,fW);
}
fclose(f);
fclose(fB);
fclose(fW);
FILE *fg = fopen("BSplot.gnu","w");
fprintf(fg,"set yrange [0:]\nset xlabel \"n\"\nset ylabel \"t\"\nplot \"BSBest.txt\" w l ti \"Best Case\", \"BSWorst.txt\" w l smooth bezier ti \"Worst Case\", \"BSAvg.txt\" w l ti \"Average Case\"\nset term png\nset output \"BSG.png\"\nreplot\nset term x11");
fclose(fg);
system("gnuplot \"BSplot.gnu\"");
system("eog BSG.png");
}