xref: /freebsd/lib/libc/stdlib/qsort.c (revision eca67d510483ad9565b7b816e16fc72ebe3b2864)
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.
1358f0484fSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
1458f0484fSRodney W. Grimes  *    must display the following acknowledgement:
1558f0484fSRodney W. Grimes  *	This product includes software developed by the University of
1658f0484fSRodney W. Grimes  *	California, Berkeley and its contributors.
1758f0484fSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
1858f0484fSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
1958f0484fSRodney W. Grimes  *    without specific prior written permission.
2058f0484fSRodney W. Grimes  *
2158f0484fSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2258f0484fSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2358f0484fSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2458f0484fSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2558f0484fSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2658f0484fSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2758f0484fSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2858f0484fSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2958f0484fSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3058f0484fSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3158f0484fSRodney W. Grimes  * SUCH DAMAGE.
3258f0484fSRodney W. Grimes  */
3358f0484fSRodney W. Grimes 
3458f0484fSRodney W. Grimes #if defined(LIBC_SCCS) && !defined(lint)
3558f0484fSRodney W. Grimes static char sccsid[] = "@(#)qsort.c	8.1 (Berkeley) 6/4/93";
3658f0484fSRodney W. Grimes #endif /* LIBC_SCCS and not lint */
37333fc21eSDavid E. O'Brien #include <sys/cdefs.h>
38333fc21eSDavid E. O'Brien __FBSDID("$FreeBSD$");
3958f0484fSRodney W. Grimes 
4058f0484fSRodney W. Grimes #include <stdlib.h>
4158f0484fSRodney W. Grimes 
42eca67d51SGarrett Wollman #ifdef I_AM_QSORT_R
43eca67d51SGarrett Wollman typedef int		 cmp_t(void *, const void *, const void *);
44eca67d51SGarrett Wollman #else
45c05ac53bSDavid E. O'Brien typedef int		 cmp_t(const void *, const void *);
46eca67d51SGarrett Wollman #endif
47eca67d51SGarrett Wollman static inline char	*med3(char *, char *, char *, cmp_t *, void *);
48c05ac53bSDavid E. O'Brien static inline void	 swapfunc(char *, char *, int, int);
4958f0484fSRodney W. Grimes 
5058f0484fSRodney W. Grimes #define min(a, b)	(a) < (b) ? a : b
5158f0484fSRodney W. Grimes 
5258f0484fSRodney W. Grimes /*
5358f0484fSRodney W. Grimes  * Qsort routine from Bentley & McIlroy's "Engineering a Sort Function".
5458f0484fSRodney W. Grimes  */
5558f0484fSRodney W. Grimes #define swapcode(TYPE, parmi, parmj, n) { 		\
5658f0484fSRodney W. Grimes 	long i = (n) / sizeof (TYPE); 			\
578fb3f3f6SDavid E. O'Brien 	TYPE *pi = (TYPE *) (parmi); 		\
588fb3f3f6SDavid E. O'Brien 	TYPE *pj = (TYPE *) (parmj); 		\
5958f0484fSRodney W. Grimes 	do { 						\
608fb3f3f6SDavid E. O'Brien 		TYPE	t = *pi;		\
6158f0484fSRodney W. Grimes 		*pi++ = *pj;				\
6258f0484fSRodney W. Grimes 		*pj++ = t;				\
6358f0484fSRodney W. Grimes         } while (--i > 0);				\
6458f0484fSRodney W. Grimes }
6558f0484fSRodney W. Grimes 
6658f0484fSRodney W. Grimes #define SWAPINIT(a, es) swaptype = ((char *)a - (char *)0) % sizeof(long) || \
6758f0484fSRodney W. Grimes 	es % sizeof(long) ? 2 : es == sizeof(long)? 0 : 1;
6858f0484fSRodney W. Grimes 
6958f0484fSRodney W. Grimes static inline void
7058f0484fSRodney W. Grimes swapfunc(a, b, n, swaptype)
7158f0484fSRodney W. Grimes 	char *a, *b;
7258f0484fSRodney W. Grimes 	int n, swaptype;
7358f0484fSRodney W. Grimes {
7458f0484fSRodney W. Grimes 	if(swaptype <= 1)
7558f0484fSRodney W. Grimes 		swapcode(long, a, b, n)
7658f0484fSRodney W. Grimes 	else
7758f0484fSRodney W. Grimes 		swapcode(char, a, b, n)
7858f0484fSRodney W. Grimes }
7958f0484fSRodney W. Grimes 
8058f0484fSRodney W. Grimes #define swap(a, b)					\
8158f0484fSRodney W. Grimes 	if (swaptype == 0) {				\
8258f0484fSRodney W. Grimes 		long t = *(long *)(a);			\
8358f0484fSRodney W. Grimes 		*(long *)(a) = *(long *)(b);		\
8458f0484fSRodney W. Grimes 		*(long *)(b) = t;			\
8558f0484fSRodney W. Grimes 	} else						\
8658f0484fSRodney W. Grimes 		swapfunc(a, b, es, swaptype)
8758f0484fSRodney W. Grimes 
8858f0484fSRodney W. Grimes #define vecswap(a, b, n) 	if ((n) > 0) swapfunc(a, b, n, swaptype)
8958f0484fSRodney W. Grimes 
90eca67d51SGarrett Wollman #ifdef I_AM_QSORT_R
91eca67d51SGarrett Wollman #define	CMP(t, x, y) (cmp((t), (x), (y)))
92eca67d51SGarrett Wollman #else
93eca67d51SGarrett Wollman #define	CMP(t, x, y) (cmp((x), (y)))
94eca67d51SGarrett Wollman #endif
95eca67d51SGarrett Wollman 
9658f0484fSRodney W. Grimes static inline char *
97eca67d51SGarrett Wollman med3(char *a, char *b, char *c, cmp_t *cmp, void *thunk
98eca67d51SGarrett Wollman #ifndef I_AM_QSORT_R
99eca67d51SGarrett Wollman __unused
100eca67d51SGarrett Wollman #endif
101eca67d51SGarrett Wollman )
10258f0484fSRodney W. Grimes {
103eca67d51SGarrett Wollman 	return CMP(thunk, a, b) < 0 ?
104eca67d51SGarrett Wollman 	       (CMP(thunk, b, c) < 0 ? b : (CMP(thunk, a, c) < 0 ? c : a ))
105eca67d51SGarrett Wollman               :(CMP(thunk, b, c) > 0 ? b : (CMP(thunk, a, c) < 0 ? a : c ));
10658f0484fSRodney W. Grimes }
10758f0484fSRodney W. Grimes 
108eca67d51SGarrett Wollman #ifdef I_AM_QSORT_R
10958f0484fSRodney W. Grimes void
110eca67d51SGarrett Wollman qsort_r(void *a, size_t n, size_t es, void *thunk, cmp_t *cmp)
111eca67d51SGarrett Wollman #else
112eca67d51SGarrett Wollman #define thunk NULL
113eca67d51SGarrett Wollman void
114eca67d51SGarrett Wollman qsort(void *a, size_t n, size_t es, cmp_t *cmp)
115eca67d51SGarrett Wollman #endif
11658f0484fSRodney W. Grimes {
11758f0484fSRodney W. Grimes 	char *pa, *pb, *pc, *pd, *pl, *pm, *pn;
11858f0484fSRodney W. Grimes 	int d, r, swaptype, swap_cnt;
11958f0484fSRodney W. Grimes 
12058f0484fSRodney W. Grimes loop:	SWAPINIT(a, es);
12158f0484fSRodney W. Grimes 	swap_cnt = 0;
12258f0484fSRodney W. Grimes 	if (n < 7) {
12309a8dfa2SBruce Evans 		for (pm = (char *)a + es; pm < (char *)a + n * es; pm += es)
124eca67d51SGarrett Wollman 			for (pl = pm;
125eca67d51SGarrett Wollman 			     pl > (char *)a && CMP(thunk, pl - es, pl) > 0;
12658f0484fSRodney W. Grimes 			     pl -= es)
12758f0484fSRodney W. Grimes 				swap(pl, pl - es);
12858f0484fSRodney W. Grimes 		return;
12958f0484fSRodney W. Grimes 	}
13009a8dfa2SBruce Evans 	pm = (char *)a + (n / 2) * es;
13158f0484fSRodney W. Grimes 	if (n > 7) {
13258f0484fSRodney W. Grimes 		pl = a;
13309a8dfa2SBruce Evans 		pn = (char *)a + (n - 1) * es;
13458f0484fSRodney W. Grimes 		if (n > 40) {
13558f0484fSRodney W. Grimes 			d = (n / 8) * es;
136eca67d51SGarrett Wollman 			pl = med3(pl, pl + d, pl + 2 * d, cmp, thunk);
137eca67d51SGarrett Wollman 			pm = med3(pm - d, pm, pm + d, cmp, thunk);
138eca67d51SGarrett Wollman 			pn = med3(pn - 2 * d, pn - d, pn, cmp, thunk);
13958f0484fSRodney W. Grimes 		}
140eca67d51SGarrett Wollman 		pm = med3(pl, pm, pn, cmp, thunk);
14158f0484fSRodney W. Grimes 	}
14258f0484fSRodney W. Grimes 	swap(a, pm);
14309a8dfa2SBruce Evans 	pa = pb = (char *)a + es;
14458f0484fSRodney W. Grimes 
14509a8dfa2SBruce Evans 	pc = pd = (char *)a + (n - 1) * es;
14658f0484fSRodney W. Grimes 	for (;;) {
147eca67d51SGarrett Wollman 		while (pb <= pc && (r = CMP(thunk, pb, a)) <= 0) {
14858f0484fSRodney W. Grimes 			if (r == 0) {
14958f0484fSRodney W. Grimes 				swap_cnt = 1;
15058f0484fSRodney W. Grimes 				swap(pa, pb);
15158f0484fSRodney W. Grimes 				pa += es;
15258f0484fSRodney W. Grimes 			}
15358f0484fSRodney W. Grimes 			pb += es;
15458f0484fSRodney W. Grimes 		}
155eca67d51SGarrett Wollman 		while (pb <= pc && (r = CMP(thunk, pc, a)) >= 0) {
15658f0484fSRodney W. Grimes 			if (r == 0) {
15758f0484fSRodney W. Grimes 				swap_cnt = 1;
15858f0484fSRodney W. Grimes 				swap(pc, pd);
15958f0484fSRodney W. Grimes 				pd -= es;
16058f0484fSRodney W. Grimes 			}
16158f0484fSRodney W. Grimes 			pc -= es;
16258f0484fSRodney W. Grimes 		}
16358f0484fSRodney W. Grimes 		if (pb > pc)
16458f0484fSRodney W. Grimes 			break;
16558f0484fSRodney W. Grimes 		swap(pb, pc);
16658f0484fSRodney W. Grimes 		swap_cnt = 1;
16758f0484fSRodney W. Grimes 		pb += es;
16858f0484fSRodney W. Grimes 		pc -= es;
16958f0484fSRodney W. Grimes 	}
17058f0484fSRodney W. Grimes 	if (swap_cnt == 0) {  /* Switch to insertion sort */
17109a8dfa2SBruce Evans 		for (pm = (char *)a + es; pm < (char *)a + n * es; pm += es)
172eca67d51SGarrett Wollman 			for (pl = pm;
173eca67d51SGarrett Wollman 			     pl > (char *)a && CMP(thunk, pl - es, pl) > 0;
17458f0484fSRodney W. Grimes 			     pl -= es)
17558f0484fSRodney W. Grimes 				swap(pl, pl - es);
17658f0484fSRodney W. Grimes 		return;
17758f0484fSRodney W. Grimes 	}
17858f0484fSRodney W. Grimes 
17909a8dfa2SBruce Evans 	pn = (char *)a + n * es;
18058f0484fSRodney W. Grimes 	r = min(pa - (char *)a, pb - pa);
18158f0484fSRodney W. Grimes 	vecswap(a, pb - r, r);
18258f0484fSRodney W. Grimes 	r = min(pd - pc, pn - pd - es);
18358f0484fSRodney W. Grimes 	vecswap(pb, pn - r, r);
18458f0484fSRodney W. Grimes 	if ((r = pb - pa) > es)
185eca67d51SGarrett Wollman #ifdef I_AM_QSORT_R
186eca67d51SGarrett Wollman 		qsort_r(a, r / es, es, thunk, cmp);
187eca67d51SGarrett Wollman #else
18858f0484fSRodney W. Grimes 		qsort(a, r / es, es, cmp);
189eca67d51SGarrett Wollman #endif
19058f0484fSRodney W. Grimes 	if ((r = pd - pc) > es) {
19158f0484fSRodney W. Grimes 		/* Iterate rather than recurse to save stack space */
19258f0484fSRodney W. Grimes 		a = pn - r;
19358f0484fSRodney W. Grimes 		n = r / es;
19458f0484fSRodney W. Grimes 		goto loop;
19558f0484fSRodney W. Grimes 	}
19658f0484fSRodney W. Grimes /*		qsort(pn - r, r / es, es, cmp);*/
19758f0484fSRodney W. Grimes }
198