1 // ----------------------------------------------------------------------------
3 // ----------------------------------------------------------------------------
4 // Public Domain C Library - http://pdclib.sourceforge.net
5 // This code is Public Domain. Use, modify, and redistribute at will.
6 // ----------------------------------------------------------------------------
9 void qsort( void * base, size_t nelem, size_t size, int (*cmp) ( const void * e1, const void * e2) ) { /* TODO */ };
11 /* PDPC code - unreviewed
12 /******************************************************************/
13 /* qsort.c -- Non-Recursive ISO C qsort() function */
15 /* Public domain by Raymond Gardner, Englewood CO February 1991 */
16 /* Minor mods by Paul Edwards also public domain */
19 /* qsort(base, nbr_elements, width_bytes, compare_function); */
21 /* size_t nbr_elements, width_bytes; */
22 /* int (*compare_function)(const void *, const void *); */
24 /* Sorts an array starting at base, of length nbr_elements, each */
25 /* element of size width_bytes, ordered via compare_function, */
26 /* which is called as (*compare_function)(ptr_to_element1, */
27 /* ptr_to_element2) and returns < 0 if element1 < element2, */
28 /* 0 if element1 = element2, > 0 if element1 > element2. */
29 /* Most refinements are due to R. Sedgewick. See "Implementing */
30 /* Quicksort Programs", Comm. ACM, Oct. 1978, and Corrigendum, */
31 /* Comm. ACM, June 1979. */
32 /******************************************************************/
35 static void swap_chars(char *, char *, size_t);
38 ** Compile with -DSWAP_INTS if your machine can access an int at an
39 ** arbitrary location with reasonable efficiency. (Some machines
40 ** cannot access an int at an odd address at all, so be careful.)
44 void swap_ints(char *, char *, size_t);
45 #define SWAP(a, b) (swap_func((char *)(a), (char *)(b), width))
47 #define SWAP(a, b) (swap_chars((char *)(a), (char *)(b), size))
50 #define COMP(a, b) ((*comp)((void *)(a), (void *)(b)))
52 #define T 7 /* subfiles of T or fewer elements will */
53 /* be sorted by a simple insertion sort */
54 /* Note! T must be at least 3 */
56 void qsort(void *basep, size_t nelems, size_t size,
57 int (*comp)(const void *, const void *))
59 char *stack[40], **sp; /* stack and stack pointer */
60 char *i, *j, *limit; /* scan and limit pointers */
61 size_t thresh; /* size of T elements in bytes */
62 char *base; /* base pointer as char * */
65 size_t width; /* width of array element */
66 void (*swap_func)(char *, char *, size_t); /* swap func pointer*/
68 width = size; /* save size for swap routine */
69 swap_func = swap_chars; /* choose swap function */
70 if ( size % sizeof(int) == 0 ) { /* size is multiple of ints */
71 width /= sizeof(int); /* set width in ints */
72 swap_func = swap_ints; /* use int swap function */
76 base = (char *)basep; /* set up char * base pointer */
77 thresh = T * size; /* init threshold */
78 sp = stack; /* init stack pointer */
79 limit = base + nelems * size;/* pointer past end of array */
80 for ( ;; ) { /* repeat until break... */
81 if ( limit - base > thresh ) { /* if more than T elements */
82 /* swap base with middle */
83 SWAP(((((size_t)(limit-base))/size)/2)*size+base, base);
84 i = base + size; /* i scans left to right */
85 j = limit - size; /* j scans right to left */
86 if ( COMP(i, j) > 0 ) /* Sedgewick's */
87 SWAP(i, j); /* three-element sort */
88 if ( COMP(base, j) > 0 ) /* sets things up */
89 SWAP(base, j); /* so that */
90 if ( COMP(i, base) > 0 ) /* *i <= *base <= *j */
91 SWAP(i, base); /* *base is pivot element */
92 for ( ;; ) { /* loop until break */
94 i += size; /* until *i >= pivot */
95 while ( COMP(i, base) < 0 );
97 j -= size; /* until *j <= pivot */
98 while ( COMP(j, base) > 0 );
99 if ( i > j ) /* if pointers crossed */
100 break; /* break loop */
101 SWAP(i, j); /* else swap elements, keep scanning*/
103 SWAP(base, j); /* move pivot into correct place */
104 if ( j - base > limit - i ) { /* if left subfile larger */
105 sp[0] = base; /* stack left subfile base */
106 sp[1] = j; /* and limit */
107 base = i; /* sort the right subfile */
108 } else { /* else right subfile larger*/
109 sp[0] = i; /* stack right subfile base */
110 sp[1] = limit; /* and limit */
111 limit = j; /* sort the left subfile */
113 sp += 2; /* increment stack pointer */
114 } else { /* else subfile is small, use insertion sort */
115 for ( j = base, i = j+size; i < limit; j = i, i += size )
116 for ( ; COMP(j, j+size) > 0; j -= size ) {
121 if ( sp != stack ) { /* if any entries on stack */
122 sp -= 2; /* pop the base and limit */
125 } else /* else stack empty, done */
132 ** swap nbytes between a and b
135 static void swap_chars(char *a, char *b, size_t nbytes)
139 tmp = *a; *a++ = *b; *b++ = tmp;
140 } while ( --nbytes );
146 ** swap nints between a and b
149 static void swap_ints(char *ap, char *bp, size_t nints)
151 int *a = (int *)ap, *b = (int *)bp;
154 tmp = *a; *a++ = *b; *b++ = tmp;