3 /* qsort( void *, size_t, size_t, int(*)( const void *, const void * ) )
5 This file is part of the Public Domain C Library (PDCLib).
6 Permission is granted to use, modify, and / or redistribute at will.
13 /* This implementation is taken from Paul Edward's PDPCLIB.
15 Original code is credited to Raymond Gardner, Englewood CO.
16 Minor mods are credited to Paul Edwards.
17 Some reformatting and simplification done by Martin Baute.
18 All code is still Public Domain.
21 /* Wrapper for _PDCLIB_memswp protects against multiple argument evaluation. */
22 static inline void memswp( char * i, char * j, size_t size )
24 _PDCLIB_memswp( i, j, size );
27 /* For small sets, insertion sort is faster than quicksort.
28 T is the threshold below which insertion sort will be used.
33 /* Macros for handling the QSort stack */
34 #define PREPARE_STACK char * stack[STACKSIZE]; char * * stackptr = stack
35 #define PUSH( base, limit ) stackptr[0] = base; stackptr[1] = limit; stackptr += 2
36 #define POP( base, limit ) stackptr -= 2; base = stackptr[0]; limit = stackptr[1]
37 /* TODO: Stack usage is log2( nmemb ) (minus what T shaves off the worst case).
38 Worst-case nmemb is platform dependent and should probably be
39 configured through _PDCLIB_config.h.
43 void qsort( void * base, size_t nmemb, size_t size, int (*compar)( const void *, const void * ) )
47 _PDCLIB_size_t thresh = T * size;
48 char * base_ = (char *)base;
49 char * limit = base_ + nmemb * size;
54 if ( (size_t)( limit - base_ ) > thresh ) /* QSort for more than T elements. */
56 /* We work from second to last - first will be pivot element. */
59 /* We swap first with middle element, then sort that with second
60 and last element so that eventually first element is the median
61 of the three - avoiding pathological pivots.
62 TODO: Instead of middle element, chose one randomly.
64 memswp( ( ( ( (size_t)( limit - base_ ) ) / size ) / 2 ) * size + base_, base_, size );
65 if ( compar( i, j ) > 0 ) memswp( i, j, size );
66 if ( compar( base_, j ) > 0 ) memswp( base_, j, size );
67 if ( compar( i, base_ ) > 0 ) memswp( i, base_, size );
68 /* Now we have the median for pivot element, entering main Quicksort. */
73 /* move i right until *i >= pivot */
75 } while ( compar( i, base_ ) < 0 );
78 /* move j left until *j <= pivot */
80 } while ( compar( j, base_ ) > 0 );
83 /* break loop if pointers crossed */
86 /* else swap elements, keep scanning */
89 /* move pivot into correct place */
90 memswp( base_, j, size );
91 /* larger subfile base / limit to stack, sort smaller */
92 if ( j - base_ > limit - i )
100 /* right is larger */
105 else /* insertion sort for less than T elements */
107 for ( j = base_, i = j + size; i < limit; j = i, i += size )
109 for ( ; compar( j, j + size ) > 0; j -= size )
111 memswp( j, j + size, size );
118 if ( stackptr != stack ) /* if any entries on stack */
122 else /* else stack empty, done */
133 #include <_PDCLIB_test.h>
137 static int compare( const void * left, const void * right )
139 return *( (unsigned char *)left ) - *( (unsigned char *)right );
144 char presort[] = { "shreicnyjqpvozxmbt" };
145 char sorted1[] = { "bcehijmnopqrstvxyz" };
146 char sorted2[] = { "bticjqnyozpvreshxm" };
148 strcpy( s, presort );
149 qsort( s, 18, 1, compare );
150 TESTCASE( strcmp( s, sorted1 ) == 0 );
151 strcpy( s, presort );
152 qsort( s, 9, 2, compare );
153 TESTCASE( strcmp( s, sorted2 ) == 0 );
154 strcpy( s, presort );
155 qsort( s, 1, 1, compare );
156 TESTCASE( strcmp( s, presort ) == 0 );
158 puts( "qsort.c: Skipping test #4 for BSD as it goes into endless loop here." );
160 qsort( s, 100, 0, compare );
161 TESTCASE( strcmp( s, presort ) == 0 );