-
Notifications
You must be signed in to change notification settings - Fork 0
/
bubblesort.c
59 lines (45 loc) · 875 Bytes
/
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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void swap (long* a, long* b)
{
long tmp = *a;
*a = *b;
*b = tmp;
}
int main()
{
char swapped = 1;
int n;
scanf ("%d", &n);
clock_t start, end;
double time;
long* nums = malloc (n * sizeof (long));
for (int i = 0; i < n; i++)
scanf ("%li", &nums[i]);
/*
* for (int i = 0; i < n; i++)
* printf ("%li ", nums[i]);
* printf ("\n");
*/
start = clock();
while (swapped != 0) {
swapped = 0;
for (int i = 0; i < n - 1; i++) {
if (nums[i] > nums[i + 1]) {
swap (&nums[i], &nums[i + 1]);
swapped = 1;
if (i % 10000 == 0)
printf("%ld passes\n", i * 100000);
}
}
}
end = clock();
time = ((double) (end - start)) / CLOCKS_PER_SEC;
printf("Time taken: %f s\n", time);
for (int i = 0; i < n; i++)
printf ("%li ", nums[i]);
printf ("\n");
free (nums);
return 0;
}