author | Markus Bröker <mbroeker@largo.dyndns.tv> |
Thu, 16 Apr 2009 12:49:13 +0200 | |
changeset 63 | 5a82f89d607e |
parent 48 | b94d657a9acb |
child 70 | ded389a5dc2a |
permissions | -rw-r--r-- |
25 | 1 |
/** |
2 |
* Comparision of Sorting Algorithms |
|
3 |
* Copyright (C) 2008 Markus Broeker |
|
4 |
*/ |
|
5 |
||
6 |
#include <stdio.h> |
|
7 |
#include <stdlib.h> |
|
8 |
#include <string.h> |
|
9 |
#include <time.h> |
|
10 |
||
11 |
#ifndef MAX |
|
12 |
#define MAX 50000 |
|
13 |
#endif |
|
14 |
||
15 |
unsigned long counter = 0; |
|
16 |
unsigned long iters = 0; |
|
17 |
||
18 |
int compare (int *a, int *b) |
|
19 |
{ |
|
20 |
iters++; |
|
21 |
if (*a > *b) { |
|
22 |
counter++; |
|
23 |
return 1; |
|
24 |
} |
|
25 |
return 0; |
|
26 |
} |
|
27 |
||
28 |
void swap (int *v, int i, int j) |
|
29 |
{ |
|
30 |
int old = v[i]; |
|
31 |
||
32 |
v[i] = v[j]; |
|
33 |
v[j] = old; |
|
34 |
} |
|
35 |
||
36 |
void statistic (char *func) |
|
37 |
{ |
|
38 |
printf ("%8s finished after %10lu swaps and %10lu Iterations.\n", func, counter, iters); |
|
39 |
counter = iters = 0; |
|
40 |
} |
|
41 |
||
42 |
/** |
|
43 |
* Laufzeitverhalten: n*(n-1) Durchläufe zum Sortieren von n Elementen... |
|
44 |
*/ |
|
45 |
void lazysort (int *v, int n, int (*compare_func) (int *, int *)) |
|
46 |
{ |
|
47 |
int i, j; |
|
48 |
||
49 |
for (i = 1; i < n; i++) { |
|
50 |
for (j = 0; j < n; j++) { |
|
51 |
if (compare_func (&v[j], &v[i])) { |
|
52 |
swap (v, i, j); |
|
53 |
} |
|
54 |
} |
|
55 |
} |
|
56 |
statistic ("Lazy"); |
|
57 |
} |
|
58 |
||
59 |
/** |
|
60 |
* Laufzeitverhalten: (1/2)*n*(n-1) Durchläufe zum Sortieren von n Elementen... |
|
61 |
*/ |
|
62 |
void bubblesort (int *v, int n, int (*compare) (int *, int *)) |
|
63 |
{ |
|
64 |
int i, j; |
|
65 |
||
66 |
for (i = (n - 1); i >= 0; i--) { |
|
67 |
for (j = 1; j <= i; j++) { |
|
68 |
if (compare (&v[j - 1], &v[j])) { |
|
69 |
swap (v, j - 1, j); |
|
70 |
} |
|
71 |
} |
|
72 |
} |
|
73 |
statistic ("Bubble"); |
|
74 |
} |
|
75 |
||
76 |
int main (int argc, char **argv) |
|
77 |
{ |
|
78 |
int a[MAX]; |
|
79 |
int b[MAX]; |
|
80 |
int c[MAX]; |
|
27
81a574d60c15
typo in min2time format string
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
25
diff
changeset
|
81 |
|
25 | 82 |
int i; |
83 |
||
84 |
srand (time (NULL)); |
|
85 |
||
86 |
for (i = 0; i < MAX; i++) { |
|
87 |
a[i] = (int)((float)MAX * rand () / (RAND_MAX + 1.0)); |
|
88 |
} |
|
89 |
||
90 |
memcpy (b, a, sizeof (a)); |
|
91 |
memcpy (c, a, sizeof (a)); |
|
92 |
||
93 |
lazysort (a, MAX, &compare); |
|
94 |
bubblesort (b, MAX, &compare); |
|
95 |
||
96 |
qsort (c, MAX, sizeof (int), (void *)&compare); |
|
97 |
statistic ("Quick"); |
|
98 |
||
99 |
printf ("PRESS <RETURN> to CONTINUE"); |
|
100 |
while (getchar () != 10); |
|
101 |
printf ("%2c %2c %2c\n--------\n", 'L', 'B', 'Q'); |
|
102 |
||
103 |
for (i = 0; i < MAX; i++) |
|
104 |
printf ("%2d %2d %2d\n", a[i], b[i], c[i]); |
|
105 |
||
48
b94d657a9acb
Policy Inonsistency on many files
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
29
diff
changeset
|
106 |
return EXIT_SUCCESS; |
25 | 107 |
} |