OpenCores
URL https://opencores.org/ocsvn/eco32/eco32/trunk

Subversion Repositories eco32

[/] [eco32/] [tags/] [eco32-0.24/] [lcc/] [tst/] [sort.c] - Diff between revs 4 and 211

Only display areas with differences | Details | Blame | View Log

Rev 4 Rev 211
int in[] = {10, 32, -1, 567, 3, 18, 1, -51, 789, 0};
int in[] = {10, 32, -1, 567, 3, 18, 1, -51, 789, 0};
 
 
main() {
main() {
        int i;
        int i;
 
 
        sort(in, (sizeof in)/(sizeof in[0]));
        sort(in, (sizeof in)/(sizeof in[0]));
        for (i = 0; i < (sizeof in)/(sizeof in[0]); i++) {
        for (i = 0; i < (sizeof in)/(sizeof in[0]); i++) {
                putd(in[i]);
                putd(in[i]);
                putchar('\n');
                putchar('\n');
        }
        }
        return 0;
        return 0;
}
}
 
 
/* putd - output decimal number */
/* putd - output decimal number */
putd(n) {
putd(n) {
        if (n < 0) {
        if (n < 0) {
                putchar('-');
                putchar('-');
                n = -n;
                n = -n;
        }
        }
        if (n/10)
        if (n/10)
                putd(n/10);
                putd(n/10);
        putchar(n%10 + '0');
        putchar(n%10 + '0');
}
}
 
 
int *xx;
int *xx;
 
 
/* sort - sort a[0..n-1] into increasing order */
/* sort - sort a[0..n-1] into increasing order */
sort(a, n) int a[]; {
sort(a, n) int a[]; {
        quick(xx = a, 0, --n);
        quick(xx = a, 0, --n);
}
}
 
 
/* quick - quicksort a[lb..ub] */
/* quick - quicksort a[lb..ub] */
quick(a, lb, ub) int a[]; {
quick(a, lb, ub) int a[]; {
        int k, partition();
        int k, partition();
 
 
        if (lb >= ub)
        if (lb >= ub)
                return;
                return;
        k = partition(a, lb, ub);
        k = partition(a, lb, ub);
        quick(a, lb, k - 1);
        quick(a, lb, k - 1);
        quick(a, k + 1, ub);
        quick(a, k + 1, ub);
}
}
 
 
/* partition - partition a[i..j] */
/* partition - partition a[i..j] */
int partition(a, i, j) int a[]; {
int partition(a, i, j) int a[]; {
        int v, k;
        int v, k;
 
 
        j++;
        j++;
        k = i;
        k = i;
        v = a[k];
        v = a[k];
        while (i < j) {
        while (i < j) {
                i++; while (a[i] < v) i++;
                i++; while (a[i] < v) i++;
                j--; while (a[j] > v) j--;
                j--; while (a[j] > v) j--;
                if (i < j) exchange(&a[i], &a[j]);
                if (i < j) exchange(&a[i], &a[j]);
        }
        }
        exchange(&a[k], &a[j]);
        exchange(&a[k], &a[j]);
        return j;
        return j;
}
}
 
 
/* exchange - exchange *x and *y */
/* exchange - exchange *x and *y */
exchange(x, y) int *x, *y; {
exchange(x, y) int *x, *y; {
        int t;
        int t;
 
 
        printf("exchange(%d,%d)\n", x - xx, y - xx);
        printf("exchange(%d,%d)\n", x - xx, y - xx);
        t = *x; *x = *y; *y = t;
        t = *x; *x = *y; *y = t;
}
}
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.