xref: /freebsd/lib/libc/stdlib/qsort.c (revision 9382fabf1f68297c14b37fbec3614c0c94274524)
158f0484fSRodney W. Grimes /*-
258f0484fSRodney W. Grimes  * Copyright (c) 1992, 1993
358f0484fSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
458f0484fSRodney W. Grimes  *
558f0484fSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
658f0484fSRodney W. Grimes  * modification, are permitted provided that the following conditions
758f0484fSRodney W. Grimes  * are met:
858f0484fSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
958f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
1058f0484fSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
1158f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
1258f0484fSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
13580b4d18SEd Maste  * 3. Neither the name of the University nor the names of its contributors
1458f0484fSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
1558f0484fSRodney W. Grimes  *    without specific prior written permission.
1658f0484fSRodney W. Grimes  *
1758f0484fSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1858f0484fSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1958f0484fSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2058f0484fSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2158f0484fSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2258f0484fSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2358f0484fSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2458f0484fSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2558f0484fSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2658f0484fSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2758f0484fSRodney W. Grimes  * SUCH DAMAGE.
2858f0484fSRodney W. Grimes  */
2958f0484fSRodney W. Grimes 
3058f0484fSRodney W. Grimes #if defined(LIBC_SCCS) && !defined(lint)
3158f0484fSRodney W. Grimes static char sccsid[] = "@(#)qsort.c	8.1 (Berkeley) 6/4/93";
3258f0484fSRodney W. Grimes #endif /* LIBC_SCCS and not lint */
33333fc21eSDavid E. O'Brien #include <sys/cdefs.h>
34333fc21eSDavid E. O'Brien __FBSDID("$FreeBSD$");
3558f0484fSRodney W. Grimes 
3658f0484fSRodney W. Grimes #include <stdlib.h>
3758f0484fSRodney W. Grimes 
38eca67d51SGarrett Wollman #ifdef I_AM_QSORT_R
39eca67d51SGarrett Wollman typedef int		 cmp_t(void *, const void *, const void *);
40eca67d51SGarrett Wollman #else
41c05ac53bSDavid E. O'Brien typedef int		 cmp_t(const void *, const void *);
42eca67d51SGarrett Wollman #endif
43eca67d51SGarrett Wollman static inline char	*med3(char *, char *, char *, cmp_t *, void *);
44*9382fabfSPedro F. Giffuni static inline void	 swapfunc(char *, char *, int, int, int);
4558f0484fSRodney W. Grimes 
4658f0484fSRodney W. Grimes #define min(a, b)	(a) < (b) ? a : b
4758f0484fSRodney W. Grimes 
4858f0484fSRodney W. Grimes /*
4958f0484fSRodney W. Grimes  * Qsort routine from Bentley & McIlroy's "Engineering a Sort Function".
5058f0484fSRodney W. Grimes  */
5158f0484fSRodney W. Grimes #define swapcode(TYPE, parmi, parmj, n) { 		\
5258f0484fSRodney W. Grimes 	long i = (n) / sizeof (TYPE); 			\
538fb3f3f6SDavid E. O'Brien 	TYPE *pi = (TYPE *) (parmi); 		\
548fb3f3f6SDavid E. O'Brien 	TYPE *pj = (TYPE *) (parmj); 		\
5558f0484fSRodney W. Grimes 	do { 						\
568fb3f3f6SDavid E. O'Brien 		TYPE	t = *pi;		\
5758f0484fSRodney W. Grimes 		*pi++ = *pj;				\
5858f0484fSRodney W. Grimes 		*pj++ = t;				\
5958f0484fSRodney W. Grimes         } while (--i > 0);				\
6058f0484fSRodney W. Grimes }
6158f0484fSRodney W. Grimes 
62*9382fabfSPedro F. Giffuni #define	SWAPINIT(TYPE, a, es) swaptype_ ## TYPE =	\
63*9382fabfSPedro F. Giffuni 	((char *)a - (char *)0) % sizeof(TYPE) ||	\
64*9382fabfSPedro F. Giffuni 	es % sizeof(TYPE) ? 2 : es == sizeof(TYPE) ? 0 : 1;
6558f0484fSRodney W. Grimes 
6658f0484fSRodney W. Grimes static inline void
67*9382fabfSPedro F. Giffuni swapfunc(a, b, n, swaptype_long, swaptype_int)
6858f0484fSRodney W. Grimes 	char *a, *b;
69*9382fabfSPedro F. Giffuni 	int n, swaptype_long, swaptype_int;
7058f0484fSRodney W. Grimes {
71*9382fabfSPedro F. Giffuni 	if (swaptype_long <= 1)
7258f0484fSRodney W. Grimes 		swapcode(long, a, b, n)
73*9382fabfSPedro F. Giffuni 	else if (swaptype_int <= 1)
74*9382fabfSPedro F. Giffuni 		swapcode(int, a, b, n)
7558f0484fSRodney W. Grimes 	else
7658f0484fSRodney W. Grimes 		swapcode(char, a, b, n)
7758f0484fSRodney W. Grimes }
7858f0484fSRodney W. Grimes 
7958f0484fSRodney W. Grimes #define	swap(a, b)					\
80*9382fabfSPedro F. Giffuni 	if (swaptype_long == 0) {			\
8158f0484fSRodney W. Grimes 		long t = *(long *)(a);			\
8258f0484fSRodney W. Grimes 		*(long *)(a) = *(long *)(b);		\
8358f0484fSRodney W. Grimes 		*(long *)(b) = t;			\
84*9382fabfSPedro F. Giffuni 	} else if (swaptype_int == 0) {			\
85*9382fabfSPedro F. Giffuni 		int t = *(int *)(a);			\
86*9382fabfSPedro F. Giffuni 		*(int *)(a) = *(int *)(b);		\
87*9382fabfSPedro F. Giffuni 		*(int *)(b) = t;			\
8858f0484fSRodney W. Grimes 	} else						\
89*9382fabfSPedro F. Giffuni 		swapfunc(a, b, es, swaptype_long, swaptype_int)
9058f0484fSRodney W. Grimes 
91*9382fabfSPedro F. Giffuni #define	vecswap(a, b, n)				\
92*9382fabfSPedro F. Giffuni 	if ((n) > 0) swapfunc(a, b, n, swaptype_long, swaptype_int)
9358f0484fSRodney W. Grimes 
94eca67d51SGarrett Wollman #ifdef I_AM_QSORT_R
95eca67d51SGarrett Wollman #define	CMP(t, x, y) (cmp((t), (x), (y)))
96eca67d51SGarrett Wollman #else
97eca67d51SGarrett Wollman #define	CMP(t, x, y) (cmp((x), (y)))
98eca67d51SGarrett Wollman #endif
99eca67d51SGarrett Wollman 
10058f0484fSRodney W. Grimes static inline char *
101eca67d51SGarrett Wollman med3(char *a, char *b, char *c, cmp_t *cmp, void *thunk
102eca67d51SGarrett Wollman #ifndef I_AM_QSORT_R
103eca67d51SGarrett Wollman __unused
104eca67d51SGarrett Wollman #endif
105eca67d51SGarrett Wollman )
10658f0484fSRodney W. Grimes {
107eca67d51SGarrett Wollman 	return CMP(thunk, a, b) < 0 ?
108eca67d51SGarrett Wollman 	       (CMP(thunk, b, c) < 0 ? b : (CMP(thunk, a, c) < 0 ? c : a ))
109eca67d51SGarrett Wollman               :(CMP(thunk, b, c) > 0 ? b : (CMP(thunk, a, c) < 0 ? a : c ));
11058f0484fSRodney W. Grimes }
11158f0484fSRodney W. Grimes 
112eca67d51SGarrett Wollman #ifdef I_AM_QSORT_R
11358f0484fSRodney W. Grimes void
114eca67d51SGarrett Wollman qsort_r(void *a, size_t n, size_t es, void *thunk, cmp_t *cmp)
115eca67d51SGarrett Wollman #else
116eca67d51SGarrett Wollman #define thunk NULL
117eca67d51SGarrett Wollman void
118eca67d51SGarrett Wollman qsort(void *a, size_t n, size_t es, cmp_t *cmp)
119eca67d51SGarrett Wollman #endif
12058f0484fSRodney W. Grimes {
12158f0484fSRodney W. Grimes 	char *pa, *pb, *pc, *pd, *pl, *pm, *pn;
122badf97cdSDavid Schultz 	size_t d, r;
123ac48ad2eSDavid Schultz 	int cmp_result;
124*9382fabfSPedro F. Giffuni 	int swaptype_long, swaptype_int, swap_cnt;
12558f0484fSRodney W. Grimes 
126*9382fabfSPedro F. Giffuni loop:	SWAPINIT(long, a, es);
127*9382fabfSPedro F. Giffuni 	SWAPINIT(int, a, es);
12858f0484fSRodney W. Grimes 	swap_cnt = 0;
12958f0484fSRodney W. Grimes 	if (n < 7) {
13009a8dfa2SBruce Evans 		for (pm = (char *)a + es; pm < (char *)a + n * es; pm += es)
131eca67d51SGarrett Wollman 			for (pl = pm;
132eca67d51SGarrett Wollman 			     pl > (char *)a && CMP(thunk, pl - es, pl) > 0;
13358f0484fSRodney W. Grimes 			     pl -= es)
13458f0484fSRodney W. Grimes 				swap(pl, pl - es);
13558f0484fSRodney W. Grimes 		return;
13658f0484fSRodney W. Grimes 	}
13709a8dfa2SBruce Evans 	pm = (char *)a + (n / 2) * es;
13858f0484fSRodney W. Grimes 	if (n > 7) {
13958f0484fSRodney W. Grimes 		pl = a;
14009a8dfa2SBruce Evans 		pn = (char *)a + (n - 1) * es;
14158f0484fSRodney W. Grimes 		if (n > 40) {
14258f0484fSRodney W. Grimes 			d = (n / 8) * es;
143eca67d51SGarrett Wollman 			pl = med3(pl, pl + d, pl + 2 * d, cmp, thunk);
144eca67d51SGarrett Wollman 			pm = med3(pm - d, pm, pm + d, cmp, thunk);
145eca67d51SGarrett Wollman 			pn = med3(pn - 2 * d, pn - d, pn, cmp, thunk);
14658f0484fSRodney W. Grimes 		}
147eca67d51SGarrett Wollman 		pm = med3(pl, pm, pn, cmp, thunk);
14858f0484fSRodney W. Grimes 	}
14958f0484fSRodney W. Grimes 	swap(a, pm);
15009a8dfa2SBruce Evans 	pa = pb = (char *)a + es;
15158f0484fSRodney W. Grimes 
15209a8dfa2SBruce Evans 	pc = pd = (char *)a + (n - 1) * es;
15358f0484fSRodney W. Grimes 	for (;;) {
154ac48ad2eSDavid Schultz 		while (pb <= pc && (cmp_result = CMP(thunk, pb, a)) <= 0) {
155ac48ad2eSDavid Schultz 			if (cmp_result == 0) {
15658f0484fSRodney W. Grimes 				swap_cnt = 1;
15758f0484fSRodney W. Grimes 				swap(pa, pb);
15858f0484fSRodney W. Grimes 				pa += es;
15958f0484fSRodney W. Grimes 			}
16058f0484fSRodney W. Grimes 			pb += es;
16158f0484fSRodney W. Grimes 		}
162ac48ad2eSDavid Schultz 		while (pb <= pc && (cmp_result = CMP(thunk, pc, a)) >= 0) {
163ac48ad2eSDavid Schultz 			if (cmp_result == 0) {
16458f0484fSRodney W. Grimes 				swap_cnt = 1;
16558f0484fSRodney W. Grimes 				swap(pc, pd);
16658f0484fSRodney W. Grimes 				pd -= es;
16758f0484fSRodney W. Grimes 			}
16858f0484fSRodney W. Grimes 			pc -= es;
16958f0484fSRodney W. Grimes 		}
17058f0484fSRodney W. Grimes 		if (pb > pc)
17158f0484fSRodney W. Grimes 			break;
17258f0484fSRodney W. Grimes 		swap(pb, pc);
17358f0484fSRodney W. Grimes 		swap_cnt = 1;
17458f0484fSRodney W. Grimes 		pb += es;
17558f0484fSRodney W. Grimes 		pc -= es;
17658f0484fSRodney W. Grimes 	}
17758f0484fSRodney W. Grimes 	if (swap_cnt == 0) {  /* Switch to insertion sort */
17809a8dfa2SBruce Evans 		for (pm = (char *)a + es; pm < (char *)a + n * es; pm += es)
179eca67d51SGarrett Wollman 			for (pl = pm;
180eca67d51SGarrett Wollman 			     pl > (char *)a && CMP(thunk, pl - es, pl) > 0;
18158f0484fSRodney W. Grimes 			     pl -= es)
18258f0484fSRodney W. Grimes 				swap(pl, pl - es);
18358f0484fSRodney W. Grimes 		return;
18458f0484fSRodney W. Grimes 	}
18558f0484fSRodney W. Grimes 
18609a8dfa2SBruce Evans 	pn = (char *)a + n * es;
18758f0484fSRodney W. Grimes 	r = min(pa - (char *)a, pb - pa);
18858f0484fSRodney W. Grimes 	vecswap(a, pb - r, r);
18958f0484fSRodney W. Grimes 	r = min(pd - pc, pn - pd - es);
19058f0484fSRodney W. Grimes 	vecswap(pb, pn - r, r);
19158f0484fSRodney W. Grimes 	if ((r = pb - pa) > es)
192eca67d51SGarrett Wollman #ifdef I_AM_QSORT_R
193eca67d51SGarrett Wollman 		qsort_r(a, r / es, es, thunk, cmp);
194eca67d51SGarrett Wollman #else
19558f0484fSRodney W. Grimes 		qsort(a, r / es, es, cmp);
196eca67d51SGarrett Wollman #endif
19758f0484fSRodney W. Grimes 	if ((r = pd - pc) > es) {
19858f0484fSRodney W. Grimes 		/* Iterate rather than recurse to save stack space */
19958f0484fSRodney W. Grimes 		a = pn - r;
20058f0484fSRodney W. Grimes 		n = r / es;
20158f0484fSRodney W. Grimes 		goto loop;
20258f0484fSRodney W. Grimes 	}
20358f0484fSRodney W. Grimes /*		qsort(pn - r, r / es, es, cmp);*/
20458f0484fSRodney W. Grimes }
205