-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbubblesort.c
More file actions
67 lines (53 loc) · 1.56 KB
/
Copy pathbubblesort.c
File metadata and controls
67 lines (53 loc) · 1.56 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
#include "bubblesort.h"
void bubblesort(int amount, double *timeInSeconds){
node* nodeArray = randomizedArray(amount);
srand(time(0));
printf("-----------------------\n");
printf("Array for bubblesort: \n");
for(int i = 0; i < amount; i++){
node* current = nodeArray + i;
printf("%d,", current->number);
if((i + 1) % 15 == 0){
printf("\n");
}
if(i == amount - 1){
printf("\n");
}
}
printf("---------------------\n");
printf("Bubblesorting...\n");
clock_t start, end;
start = clock();
int sorted = 0;
while(sorted == 0){
int swaps = 0;
for(int i = 0; i < amount; i++){
if(i != amount - 1){
node* current = nodeArray + i;
node* next = nodeArray + i +1;
if(current->number > next->number){
int temp = next->number;
next->number = current->number;
current->number = temp;
swaps++;
}
}
}
if(swaps == 0){
sorted = 1;
}
}
end = clock();
*timeInSeconds = ((double) (end - start)) / CLOCKS_PER_SEC;
printf("Time used by bubblesort: %f seconds\n", *timeInSeconds);
printf("---------------------\n");
for(int i = 0; i < amount; i++){
node* current = nodeArray + i;
printf("%d,", current->number);
if((i + 1) % 15 == 0){
printf("\n");
}
}
printf("\n");
free(nodeArray);
}