Chameleon

Chameleon Svn Source Tree

Root/tags/2.0/i386/modules/klibc/qsort.c

Source at commit 1834 created 12 years 4 months ago.
By blackosx, Fix (patch) building process. Resources now download and build successfully so package can be made. Checks are now needed to see if this works on different localizations and different systems. I have a feeling more work weill be needed to handle the unicodes etc.
1/*
2 * qsort.c
3 *
4 * This is actually combsort. It's an O(n log n) algorithm with
5 * simplicity/small code size being its main virtue.
6 */
7
8#include <stddef.h>
9#include <stdlib.h>
10#include <string.h>
11
12extern void memswap(void *m1, void *m2, size_t n);
13
14static inline size_t newgap(size_t gap)
15{
16gap = (gap * 10) / 13;
17if (gap == 9 || gap == 10)
18gap = 11;
19
20if (gap < 1)
21gap = 1;
22return gap;
23}
24
25void qsort(void *base, size_t nmemb, size_t size,
26 int (*compar) (const void *, const void *))
27{
28size_t gap = nmemb;
29size_t i, j;
30char *p1, *p2;
31int swapped;
32
33if (!nmemb)
34return;
35
36do {
37gap = newgap(gap);
38swapped = 0;
39
40for (i = 0, p1 = base; i < nmemb - gap; i++, p1 += size) {
41j = i + gap;
42if (compar(p1, p2 = (char *)base + j * size) > 0) {
43memswap(p1, p2, size);
44swapped = 1;
45}
46}
47} while (gap > 1 || swapped);
48}
49

Archive Download this file

Revision: 1834