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