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