-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbubblesort-c
More file actions
94 lines (84 loc) · 2.39 KB
/
bubblesort-c
File metadata and controls
94 lines (84 loc) · 2.39 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void bubbleSort(int a[] , int n){
int temp;
for(int i=0; i<=n-2; i++)
{
for(int j=0; j<=n-2-i; j++)
{
if (a[j+1]<= a[j])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
}
//function to generate randon numbers between 0 - 9999
void generateRandomNumbers(int a[],int n){
for(int i=0;i<n;i++){
a[i]=rand()%10000;
}
}
// int main()
// {
// int a[10];
// int n;
// printf("Enter the number of elements to be sorted : ");
// scanf("%d",&n);
// printf("Enter the elements :\n" ) ;
// for (int i=0;i<n;i++)
// scanf("%d",&a[i] );
// printf("Elements before sorting :");
// for (int i=0;i<n;i++)
// printf("%d");
// bubbleSort(a,n);
// printf("Elements after sorting :");
// for (int i=0;i<n;i++)
// printf("%d");
// }
int main()
{
int a[10000];
clock_t start , end;
double time_taken; // float 6 digits after decimal but double 8 digits after decimal
double theoretical_time;
FILE *fp;
// srand(time(NULL)); // seed value for random number generation
srand((unsigned)time(NULL));
fp=fopen("b_time.txt", "w");
if(fp==NULL)
{
printf("File cannot be opened\n");
return 1;
}
for(int n=100;n<10000;n+=100){
generateRandomNumbers(a,n);
start=clock();
bubbleSort(a,n);
end=clock();
time_taken=(double) (end - start)/CLOCKS_PER_SEC;
theoretical_time= (double)(n*n)*1e-8; // multiplied with 0.0001 to get
fprintf(fp,"%d %lf %lf\n",n,time_taken,theoretical_time); }
fclose(fp);
FILE *gP = popen("gnuplot -persistent", "w");
if(gP == NULL)
{
printf("Gnuplot not found\n");
return 1;
}
fprintf(gP, "set title 'Bubble Sort Time Efficienc' \n ");
fprintf(gP, "set xlabel Input Size \n");
fprintf(gP, "set ylabel Time Complexity \n ");
fprintf(gP, "set grid \n ");
fprintf(gP, "set xlabel 'Input Size' \n ");
fprintf(gP, "set term png\n");
fprintf(gP, "set output 'B_sort_efficiency_c.png' \n");
fprintf(gP, "plot 'b_time.txt' using 1:2 with linespoints title 'Actual Time',");
fprintf(gP, "'b_time.txt' using 1:3 with lines lw 2 title 'Theoretical Time'\n");
fflush(gP);
pclose(gP);
return 0;
}