forked from MAYANK25402/Hactober-2023-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBubbleSort.c
68 lines (64 loc) · 1.48 KB
/
BubbleSort.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
// Program - Bubble Sort
#include <stdio.h>
#include<stdlib.h>
#include <time.h>
int array[1000000];
float timedifference_msec(struct timeval t0, struct timeval t1)
{
return (t1.tv_sec - t0.tv_sec) * 1000.0f + (t1.tv_usec - t0.tv_usec) / 1000.0f;
}
int main()
{
struct timeval t0;
struct timeval t1;
float elapsed;
int i,key,ch;
long size,limit;
printf("Enter number of Elements: ");
scanf("%ld",&size);
int array[size];
printf("Enter Upper limit in place values: ");
scanf("%ld",&limit);
int num=size;
int j,x,y,temp;
srand( (unsigned) time(NULL) * getpid());
gettimeofday(&t0, NULL);
if(array != NULL)
{
for(j = 0; j < num; j++)
{
array[j] = rand()%limit;
}
}
gettimeofday(&t1, NULL);
elapsed = timedifference_msec(t0, t1);
printf("\n");
printf("List of Numbers:\n");
for(j = 0; j < num; j++)
{
printf("%d\n",array[j]);
}
printf("Random Generation code executed in %f milliseconds.\n", elapsed);
printf("\n");
gettimeofday(&t0, NULL);
for(x = 0; x < num - 1; x++)
{
for(y = 0; y < num - x - 1; y++)
{
if(array[y] > array[y + 1])
{
temp = array[y];
array[y] = array[y + 1];
array[y + 1] = temp;
}
}
}
gettimeofday(&t1, NULL);
elapsed = timedifference_msec(t0, t1);
printf("Bubble Sorted List of Numbers:\n");
for(j = 0; j < num; j++)
{
printf("%d\n",array[j]);
}
printf("Bubble Sort Code executed in %f milliseconds.\n", elapsed);
}