xref: /freebsd/lib/libc/stdlib/qsort.c (revision 8a16b7a18f5d0b031f09832fd7752fba717e2a97)
158f0484fSRodney W. Grimes /*-
2*8a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3*8a16b7a1SPedro F. Giffuni  *
458f0484fSRodney W. Grimes  * Copyright (c) 1992, 1993
558f0484fSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
658f0484fSRodney W. Grimes  *
758f0484fSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
858f0484fSRodney W. Grimes  * modification, are permitted provided that the following conditions
958f0484fSRodney W. Grimes  * are met:
1058f0484fSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
1158f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
1258f0484fSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
1358f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
1458f0484fSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
15580b4d18SEd Maste  * 3. Neither the name of the University nor the names of its contributors
1658f0484fSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
1758f0484fSRodney W. Grimes  *    without specific prior written permission.
1858f0484fSRodney W. Grimes  *
1958f0484fSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2058f0484fSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2158f0484fSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2258f0484fSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2358f0484fSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2458f0484fSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2558f0484fSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2658f0484fSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2758f0484fSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2858f0484fSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2958f0484fSRodney W. Grimes  * SUCH DAMAGE.
3058f0484fSRodney W. Grimes  */
3158f0484fSRodney W. Grimes 
3258f0484fSRodney W. Grimes #if defined(LIBC_SCCS) && !defined(lint)
3358f0484fSRodney W. Grimes static char sccsid[] = "@(#)qsort.c	8.1 (Berkeley) 6/4/93";
3458f0484fSRodney W. Grimes #endif /* LIBC_SCCS and not lint */
35333fc21eSDavid E. O'Brien #include <sys/cdefs.h>
36333fc21eSDavid E. O'Brien __FBSDID("$FreeBSD$");
3758f0484fSRodney W. Grimes 
3858f0484fSRodney W. Grimes #include <stdlib.h>
3958f0484fSRodney W. Grimes 
40eca67d51SGarrett Wollman #ifdef I_AM_QSORT_R
41eca67d51SGarrett Wollman typedef int		 cmp_t(void *, const void *, const void *);
42eca67d51SGarrett Wollman #else
43c05ac53bSDavid E. O'Brien typedef int		 cmp_t(const void *, const void *);
44eca67d51SGarrett Wollman #endif
45eca67d51SGarrett Wollman static inline char	*med3(char *, char *, char *, cmp_t *, void *);
46a3f893fcSXin LI static inline void	 swapfunc(char *, char *, size_t, int, int);
4758f0484fSRodney W. Grimes 
482eaea119SPedro F. Giffuni #define	MIN(a, b)	((a) < (b) ? a : b)
4958f0484fSRodney W. Grimes 
5058f0484fSRodney W. Grimes /*
5158f0484fSRodney W. Grimes  * Qsort routine from Bentley & McIlroy's "Engineering a Sort Function".
5258f0484fSRodney W. Grimes  */
5358f0484fSRodney W. Grimes #define	swapcode(TYPE, parmi, parmj, n) {		\
54a3f893fcSXin LI 	size_t i = (n) / sizeof (TYPE);			\
558fb3f3f6SDavid E. O'Brien 	TYPE *pi = (TYPE *) (parmi);		\
568fb3f3f6SDavid E. O'Brien 	TYPE *pj = (TYPE *) (parmj);		\
5758f0484fSRodney W. Grimes 	do { 						\
588fb3f3f6SDavid E. O'Brien 		TYPE	t = *pi;		\
5958f0484fSRodney W. Grimes 		*pi++ = *pj;				\
6058f0484fSRodney W. Grimes 		*pj++ = t;				\
6158f0484fSRodney W. Grimes 	} while (--i > 0);				\
6258f0484fSRodney W. Grimes }
6358f0484fSRodney W. Grimes 
649382fabfSPedro F. Giffuni #define	SWAPINIT(TYPE, a, es) swaptype_ ## TYPE =	\
659382fabfSPedro F. Giffuni 	((char *)a - (char *)0) % sizeof(TYPE) ||	\
669382fabfSPedro F. Giffuni 	es % sizeof(TYPE) ? 2 : es == sizeof(TYPE) ? 0 : 1;
6758f0484fSRodney W. Grimes 
6858f0484fSRodney W. Grimes static inline void
69a3f893fcSXin LI swapfunc(char *a, char *b, size_t n, int swaptype_long, int swaptype_int)
7058f0484fSRodney W. Grimes {
719382fabfSPedro F. Giffuni 	if (swaptype_long <= 1)
7258f0484fSRodney W. Grimes 		swapcode(long, a, b, n)
739382fabfSPedro F. Giffuni 	else if (swaptype_int <= 1)
749382fabfSPedro 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)					\
809382fabfSPedro 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;			\
849382fabfSPedro F. Giffuni 	} else if (swaptype_int == 0) {			\
859382fabfSPedro F. Giffuni 		int t = *(int *)(a);			\
869382fabfSPedro F. Giffuni 		*(int *)(a) = *(int *)(b);		\
879382fabfSPedro F. Giffuni 		*(int *)(b) = t;			\
8858f0484fSRodney W. Grimes 	} else						\
899382fabfSPedro F. Giffuni 		swapfunc(a, b, es, swaptype_long, swaptype_int)
9058f0484fSRodney W. Grimes 
919382fabfSPedro F. Giffuni #define	vecswap(a, b, n)				\
929382fabfSPedro 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;
122ca1578f0SXin LI 	size_t d1, d2;
123ac48ad2eSDavid Schultz 	int cmp_result;
1249382fabfSPedro F. Giffuni 	int swaptype_long, swaptype_int, swap_cnt;
12558f0484fSRodney W. Grimes 
1269382fabfSPedro F. Giffuni loop:	SWAPINIT(long, a, es);
1279382fabfSPedro 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) {
142ca1578f0SXin LI 			size_t d = (n / 8) * es;
143ca1578f0SXin LI 
144eca67d51SGarrett Wollman 			pl = med3(pl, pl + d, pl + 2 * d, cmp, thunk);
145eca67d51SGarrett Wollman 			pm = med3(pm - d, pm, pm + d, cmp, thunk);
146eca67d51SGarrett Wollman 			pn = med3(pn - 2 * d, pn - d, pn, cmp, thunk);
14758f0484fSRodney W. Grimes 		}
148eca67d51SGarrett Wollman 		pm = med3(pl, pm, pn, cmp, thunk);
14958f0484fSRodney W. Grimes 	}
15058f0484fSRodney W. Grimes 	swap(a, pm);
15109a8dfa2SBruce Evans 	pa = pb = (char *)a + es;
15258f0484fSRodney W. Grimes 
15309a8dfa2SBruce Evans 	pc = pd = (char *)a + (n - 1) * es;
15458f0484fSRodney W. Grimes 	for (;;) {
155ac48ad2eSDavid Schultz 		while (pb <= pc && (cmp_result = CMP(thunk, pb, a)) <= 0) {
156ac48ad2eSDavid Schultz 			if (cmp_result == 0) {
15758f0484fSRodney W. Grimes 				swap_cnt = 1;
15858f0484fSRodney W. Grimes 				swap(pa, pb);
15958f0484fSRodney W. Grimes 				pa += es;
16058f0484fSRodney W. Grimes 			}
16158f0484fSRodney W. Grimes 			pb += es;
16258f0484fSRodney W. Grimes 		}
163ac48ad2eSDavid Schultz 		while (pb <= pc && (cmp_result = CMP(thunk, pc, a)) >= 0) {
164ac48ad2eSDavid Schultz 			if (cmp_result == 0) {
16558f0484fSRodney W. Grimes 				swap_cnt = 1;
16658f0484fSRodney W. Grimes 				swap(pc, pd);
16758f0484fSRodney W. Grimes 				pd -= es;
16858f0484fSRodney W. Grimes 			}
16958f0484fSRodney W. Grimes 			pc -= es;
17058f0484fSRodney W. Grimes 		}
17158f0484fSRodney W. Grimes 		if (pb > pc)
17258f0484fSRodney W. Grimes 			break;
17358f0484fSRodney W. Grimes 		swap(pb, pc);
17458f0484fSRodney W. Grimes 		swap_cnt = 1;
17558f0484fSRodney W. Grimes 		pb += es;
17658f0484fSRodney W. Grimes 		pc -= es;
17758f0484fSRodney W. Grimes 	}
17858f0484fSRodney W. Grimes 	if (swap_cnt == 0) {  /* Switch to insertion sort */
17909a8dfa2SBruce Evans 		for (pm = (char *)a + es; pm < (char *)a + n * es; pm += es)
180eca67d51SGarrett Wollman 			for (pl = pm;
181eca67d51SGarrett Wollman 			     pl > (char *)a && CMP(thunk, pl - es, pl) > 0;
18258f0484fSRodney W. Grimes 			     pl -= es)
18358f0484fSRodney W. Grimes 				swap(pl, pl - es);
18458f0484fSRodney W. Grimes 		return;
18558f0484fSRodney W. Grimes 	}
18658f0484fSRodney W. Grimes 
18709a8dfa2SBruce Evans 	pn = (char *)a + n * es;
188ca1578f0SXin LI 	d1 = MIN(pa - (char *)a, pb - pa);
189ca1578f0SXin LI 	vecswap(a, pb - d1, d1);
190ca1578f0SXin LI 	d1 = MIN(pd - pc, pn - pd - es);
191ca1578f0SXin LI 	vecswap(pb, pn - d1, d1);
192ca1578f0SXin LI 
193ca1578f0SXin LI 	d1 = pb - pa;
194ca1578f0SXin LI 	d2 = pd - pc;
195ca1578f0SXin LI 	if (d1 <= d2) {
196ca1578f0SXin LI 		/* Recurse on left partition, then iterate on right partition */
197ca1578f0SXin LI 		if (d1 > es) {
198eca67d51SGarrett Wollman #ifdef I_AM_QSORT_R
199ca1578f0SXin LI 			qsort_r(a, d1 / es, es, thunk, cmp);
200eca67d51SGarrett Wollman #else
201ca1578f0SXin LI 			qsort(a, d1 / es, es, cmp);
202eca67d51SGarrett Wollman #endif
203ca1578f0SXin LI 		}
204ca1578f0SXin LI 		if (d2 > es) {
20558f0484fSRodney W. Grimes 			/* Iterate rather than recurse to save stack space */
206ca1578f0SXin LI 			/* qsort(pn - d2, d2 / es, es, cmp); */
207ca1578f0SXin LI 			a = pn - d2;
208ca1578f0SXin LI 			n = d2 / es;
20958f0484fSRodney W. Grimes 			goto loop;
21058f0484fSRodney W. Grimes 		}
211ca1578f0SXin LI 	} else {
212ca1578f0SXin LI 		/* Recurse on right partition, then iterate on left partition */
213ca1578f0SXin LI 		if (d2 > es) {
214ca1578f0SXin LI #ifdef I_AM_QSORT_R
215ca1578f0SXin LI 			qsort_r(pn - d2, d2 / es, es, thunk, cmp);
216ca1578f0SXin LI #else
217ca1578f0SXin LI 			qsort(pn - d2, d2 / es, es, cmp);
218ca1578f0SXin LI #endif
219ca1578f0SXin LI 		}
220ca1578f0SXin LI 		if (d1 > es) {
221ca1578f0SXin LI 			/* Iterate rather than recurse to save stack space */
222ca1578f0SXin LI 			/* qsort(a, d1 / es, es, cmp); */
223ca1578f0SXin LI 			n = d1 / es;
224ca1578f0SXin LI 			goto loop;
225ca1578f0SXin LI 		}
226ca1578f0SXin LI 	}
22758f0484fSRodney W. Grimes }
228