xref: /freebsd/lib/libc/stdlib/qsort.c (revision ca1578f0c013f6e0e88142ffa647724d64a66fb3)
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 *);
44a3f893fcSXin LI static inline void	 swapfunc(char *, char *, size_t, int, int);
4558f0484fSRodney W. Grimes 
462eaea119SPedro F. Giffuni #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) {		\
52a3f893fcSXin LI 	size_t 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 
629382fabfSPedro F. Giffuni #define	SWAPINIT(TYPE, a, es) swaptype_ ## TYPE =	\
639382fabfSPedro F. Giffuni 	((char *)a - (char *)0) % sizeof(TYPE) ||	\
649382fabfSPedro F. Giffuni 	es % sizeof(TYPE) ? 2 : es == sizeof(TYPE) ? 0 : 1;
6558f0484fSRodney W. Grimes 
6658f0484fSRodney W. Grimes static inline void
67a3f893fcSXin LI swapfunc(char *a, char *b, size_t n, int swaptype_long, int swaptype_int)
6858f0484fSRodney W. Grimes {
699382fabfSPedro F. Giffuni 	if (swaptype_long <= 1)
7058f0484fSRodney W. Grimes 		swapcode(long, a, b, n)
719382fabfSPedro F. Giffuni 	else if (swaptype_int <= 1)
729382fabfSPedro F. Giffuni 		swapcode(int, a, b, n)
7358f0484fSRodney W. Grimes 	else
7458f0484fSRodney W. Grimes 		swapcode(char, a, b, n)
7558f0484fSRodney W. Grimes }
7658f0484fSRodney W. Grimes 
7758f0484fSRodney W. Grimes #define	swap(a, b)					\
789382fabfSPedro F. Giffuni 	if (swaptype_long == 0) {			\
7958f0484fSRodney W. Grimes 		long t = *(long *)(a);			\
8058f0484fSRodney W. Grimes 		*(long *)(a) = *(long *)(b);		\
8158f0484fSRodney W. Grimes 		*(long *)(b) = t;			\
829382fabfSPedro F. Giffuni 	} else if (swaptype_int == 0) {			\
839382fabfSPedro F. Giffuni 		int t = *(int *)(a);			\
849382fabfSPedro F. Giffuni 		*(int *)(a) = *(int *)(b);		\
859382fabfSPedro F. Giffuni 		*(int *)(b) = t;			\
8658f0484fSRodney W. Grimes 	} else						\
879382fabfSPedro F. Giffuni 		swapfunc(a, b, es, swaptype_long, swaptype_int)
8858f0484fSRodney W. Grimes 
899382fabfSPedro F. Giffuni #define	vecswap(a, b, n)				\
909382fabfSPedro F. Giffuni 	if ((n) > 0) swapfunc(a, b, n, swaptype_long, swaptype_int)
9158f0484fSRodney W. Grimes 
92eca67d51SGarrett Wollman #ifdef I_AM_QSORT_R
93eca67d51SGarrett Wollman #define	CMP(t, x, y) (cmp((t), (x), (y)))
94eca67d51SGarrett Wollman #else
95eca67d51SGarrett Wollman #define	CMP(t, x, y) (cmp((x), (y)))
96eca67d51SGarrett Wollman #endif
97eca67d51SGarrett Wollman 
9858f0484fSRodney W. Grimes static inline char *
99eca67d51SGarrett Wollman med3(char *a, char *b, char *c, cmp_t *cmp, void *thunk
100eca67d51SGarrett Wollman #ifndef I_AM_QSORT_R
101eca67d51SGarrett Wollman __unused
102eca67d51SGarrett Wollman #endif
103eca67d51SGarrett Wollman )
10458f0484fSRodney W. Grimes {
105eca67d51SGarrett Wollman 	return CMP(thunk, a, b) < 0 ?
106eca67d51SGarrett Wollman 	       (CMP(thunk, b, c) < 0 ? b : (CMP(thunk, a, c) < 0 ? c : a ))
107eca67d51SGarrett Wollman 	      :(CMP(thunk, b, c) > 0 ? b : (CMP(thunk, a, c) < 0 ? a : c ));
10858f0484fSRodney W. Grimes }
10958f0484fSRodney W. Grimes 
110eca67d51SGarrett Wollman #ifdef I_AM_QSORT_R
11158f0484fSRodney W. Grimes void
112eca67d51SGarrett Wollman qsort_r(void *a, size_t n, size_t es, void *thunk, cmp_t *cmp)
113eca67d51SGarrett Wollman #else
114eca67d51SGarrett Wollman #define	thunk NULL
115eca67d51SGarrett Wollman void
116eca67d51SGarrett Wollman qsort(void *a, size_t n, size_t es, cmp_t *cmp)
117eca67d51SGarrett Wollman #endif
11858f0484fSRodney W. Grimes {
11958f0484fSRodney W. Grimes 	char *pa, *pb, *pc, *pd, *pl, *pm, *pn;
120*ca1578f0SXin LI 	size_t d1, d2;
121ac48ad2eSDavid Schultz 	int cmp_result;
1229382fabfSPedro F. Giffuni 	int swaptype_long, swaptype_int, swap_cnt;
12358f0484fSRodney W. Grimes 
1249382fabfSPedro F. Giffuni loop:	SWAPINIT(long, a, es);
1259382fabfSPedro F. Giffuni 	SWAPINIT(int, a, es);
12658f0484fSRodney W. Grimes 	swap_cnt = 0;
12758f0484fSRodney W. Grimes 	if (n < 7) {
12809a8dfa2SBruce Evans 		for (pm = (char *)a + es; pm < (char *)a + n * es; pm += es)
129eca67d51SGarrett Wollman 			for (pl = pm;
130eca67d51SGarrett Wollman 			     pl > (char *)a && CMP(thunk, pl - es, pl) > 0;
13158f0484fSRodney W. Grimes 			     pl -= es)
13258f0484fSRodney W. Grimes 				swap(pl, pl - es);
13358f0484fSRodney W. Grimes 		return;
13458f0484fSRodney W. Grimes 	}
13509a8dfa2SBruce Evans 	pm = (char *)a + (n / 2) * es;
13658f0484fSRodney W. Grimes 	if (n > 7) {
13758f0484fSRodney W. Grimes 		pl = a;
13809a8dfa2SBruce Evans 		pn = (char *)a + (n - 1) * es;
13958f0484fSRodney W. Grimes 		if (n > 40) {
140*ca1578f0SXin LI 			size_t d = (n / 8) * es;
141*ca1578f0SXin LI 
142eca67d51SGarrett Wollman 			pl = med3(pl, pl + d, pl + 2 * d, cmp, thunk);
143eca67d51SGarrett Wollman 			pm = med3(pm - d, pm, pm + d, cmp, thunk);
144eca67d51SGarrett Wollman 			pn = med3(pn - 2 * d, pn - d, pn, cmp, thunk);
14558f0484fSRodney W. Grimes 		}
146eca67d51SGarrett Wollman 		pm = med3(pl, pm, pn, cmp, thunk);
14758f0484fSRodney W. Grimes 	}
14858f0484fSRodney W. Grimes 	swap(a, pm);
14909a8dfa2SBruce Evans 	pa = pb = (char *)a + es;
15058f0484fSRodney W. Grimes 
15109a8dfa2SBruce Evans 	pc = pd = (char *)a + (n - 1) * es;
15258f0484fSRodney W. Grimes 	for (;;) {
153ac48ad2eSDavid Schultz 		while (pb <= pc && (cmp_result = CMP(thunk, pb, a)) <= 0) {
154ac48ad2eSDavid Schultz 			if (cmp_result == 0) {
15558f0484fSRodney W. Grimes 				swap_cnt = 1;
15658f0484fSRodney W. Grimes 				swap(pa, pb);
15758f0484fSRodney W. Grimes 				pa += es;
15858f0484fSRodney W. Grimes 			}
15958f0484fSRodney W. Grimes 			pb += es;
16058f0484fSRodney W. Grimes 		}
161ac48ad2eSDavid Schultz 		while (pb <= pc && (cmp_result = CMP(thunk, pc, a)) >= 0) {
162ac48ad2eSDavid Schultz 			if (cmp_result == 0) {
16358f0484fSRodney W. Grimes 				swap_cnt = 1;
16458f0484fSRodney W. Grimes 				swap(pc, pd);
16558f0484fSRodney W. Grimes 				pd -= es;
16658f0484fSRodney W. Grimes 			}
16758f0484fSRodney W. Grimes 			pc -= es;
16858f0484fSRodney W. Grimes 		}
16958f0484fSRodney W. Grimes 		if (pb > pc)
17058f0484fSRodney W. Grimes 			break;
17158f0484fSRodney W. Grimes 		swap(pb, pc);
17258f0484fSRodney W. Grimes 		swap_cnt = 1;
17358f0484fSRodney W. Grimes 		pb += es;
17458f0484fSRodney W. Grimes 		pc -= es;
17558f0484fSRodney W. Grimes 	}
17658f0484fSRodney W. Grimes 	if (swap_cnt == 0) {  /* Switch to insertion sort */
17709a8dfa2SBruce Evans 		for (pm = (char *)a + es; pm < (char *)a + n * es; pm += es)
178eca67d51SGarrett Wollman 			for (pl = pm;
179eca67d51SGarrett Wollman 			     pl > (char *)a && CMP(thunk, pl - es, pl) > 0;
18058f0484fSRodney W. Grimes 			     pl -= es)
18158f0484fSRodney W. Grimes 				swap(pl, pl - es);
18258f0484fSRodney W. Grimes 		return;
18358f0484fSRodney W. Grimes 	}
18458f0484fSRodney W. Grimes 
18509a8dfa2SBruce Evans 	pn = (char *)a + n * es;
186*ca1578f0SXin LI 	d1 = MIN(pa - (char *)a, pb - pa);
187*ca1578f0SXin LI 	vecswap(a, pb - d1, d1);
188*ca1578f0SXin LI 	d1 = MIN(pd - pc, pn - pd - es);
189*ca1578f0SXin LI 	vecswap(pb, pn - d1, d1);
190*ca1578f0SXin LI 
191*ca1578f0SXin LI 	d1 = pb - pa;
192*ca1578f0SXin LI 	d2 = pd - pc;
193*ca1578f0SXin LI 	if (d1 <= d2) {
194*ca1578f0SXin LI 		/* Recurse on left partition, then iterate on right partition */
195*ca1578f0SXin LI 		if (d1 > es) {
196eca67d51SGarrett Wollman #ifdef I_AM_QSORT_R
197*ca1578f0SXin LI 			qsort_r(a, d1 / es, es, thunk, cmp);
198eca67d51SGarrett Wollman #else
199*ca1578f0SXin LI 			qsort(a, d1 / es, es, cmp);
200eca67d51SGarrett Wollman #endif
201*ca1578f0SXin LI 		}
202*ca1578f0SXin LI 		if (d2 > es) {
20358f0484fSRodney W. Grimes 			/* Iterate rather than recurse to save stack space */
204*ca1578f0SXin LI 			/* qsort(pn - d2, d2 / es, es, cmp); */
205*ca1578f0SXin LI 			a = pn - d2;
206*ca1578f0SXin LI 			n = d2 / es;
20758f0484fSRodney W. Grimes 			goto loop;
20858f0484fSRodney W. Grimes 		}
209*ca1578f0SXin LI 	} else {
210*ca1578f0SXin LI 		/* Recurse on right partition, then iterate on left partition */
211*ca1578f0SXin LI 		if (d2 > es) {
212*ca1578f0SXin LI #ifdef I_AM_QSORT_R
213*ca1578f0SXin LI 			qsort_r(pn - d2, d2 / es, es, thunk, cmp);
214*ca1578f0SXin LI #else
215*ca1578f0SXin LI 			qsort(pn - d2, d2 / es, es, cmp);
216*ca1578f0SXin LI #endif
217*ca1578f0SXin LI 		}
218*ca1578f0SXin LI 		if (d1 > es) {
219*ca1578f0SXin LI 			/* Iterate rather than recurse to save stack space */
220*ca1578f0SXin LI 			/* qsort(a, d1 / es, es, cmp); */
221*ca1578f0SXin LI 			n = d1 / es;
222*ca1578f0SXin LI 			goto loop;
223*ca1578f0SXin LI 		}
224*ca1578f0SXin LI 	}
22558f0484fSRodney W. Grimes }
226