-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbubble.c
More file actions
115 lines (105 loc) · 2.17 KB
/
Copy pathbubble.c
File metadata and controls
115 lines (105 loc) · 2.17 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/* Bubbule sort as example of conditional operations */
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include "clock.h"
void bubble1(int a[], int n) {
int i, j;
for (i = n-1; i > 0; i--)
for (j = 0; j < i; j++) {
if (a[j] > a[j+1]) {
int t = a[j];
a[j] = a[j+1];
a[j+1] = t;
}
}
}
void bubble2(int a[], int n) {
int i, j;
for (i = n-1; i > 0; i--)
for (j = 0; j < i; j++) {
int naj = a[j] > a[j+1] ? a[j+1] : a[j];
int naj1 = a[j] > a[j+1] ? a[j] : a[j+1];
a[j] = naj;
a[j+1] = naj1;
}
}
int bubcnt(int a[], int n) {
int i, j;
int comparisons = 0;
int swaps = 0;
for (i = n-1; i > 0; i--)
for (j = 0; j < i; j++) {
comparisons++;
if (a[j] > a[j+1]) {
int t = a[j];
a[j] = a[j+1];
a[j+1] = t;
swaps++;
}
}
printf("%d comparisions, %d swaps, ratio = %.2f\n",
comparisons, swaps, (double) swaps/comparisons);
return comparisons;
}
#define LEN 100
int a0[LEN], a1[LEN], a2[LEN];
void init(unsigned seed) {
int i;
if (seed <= 1) {
int val = 0;
int incr = seed ? 1 : -1;
for (i = 0; i < LEN; i++) {
a0[i] = a1[i] = a2[i] = val+=incr;
}
} else {
srandom(seed);
for (i = 0; i < LEN; i++) {
a0[i] = a1[i] = a2[i] = random();
}
}
}
void show(char *label, int a[], int n) {
int i;
printf("%s: ", label);
for (i = 0; i < n; i++)
printf("%d ", a[i]);
printf("\n");
}
void run(int len, int trials, unsigned seed) {
double t1, t2;
int t, comp = 0;
for (t = 0; t < trials; t++) {
init(seed);
if (t == 0) {
if (len <= 10) {
show("Before ", a1, len);
}
comp = bubcnt(a0, len);
}
start_counter();
bubble1(a1, len);
t1 = get_counter();
start_counter();
bubble2(a2, len);
t2 = get_counter();
if (t == 0 && len <= 10) {
show("Bubble1", a1, len);
show("Bubble2", a2, len);
}
printf("%d\t%.2f\t%.2f\n", len, t1/comp, t2/comp);
}
}
int main(int argc, char *argv[]) {
int len = LEN;
int trials = 3;
unsigned seed = 1;
if (argc > 1)
len = atoi(argv[1]);
if (argc > 2)
trials = atoi(argv[2]);
if (argc > 3)
seed = atoi(argv[3]);
run(len, trials, seed);
return 0;
}