xref: /openbsd-src/lib/libc/stdlib/qsort.c (revision 7d863ca839cb05b88652bf4c947e5d158a411f6d)
1*7d863ca8Smillert /*	$OpenBSD: qsort.c,v 1.18 2017/05/30 14:54:09 millert Exp $ */
2df930be7Sderaadt /*-
3df930be7Sderaadt  * Copyright (c) 1992, 1993
4df930be7Sderaadt  *	The Regents of the University of California.  All rights reserved.
5df930be7Sderaadt  *
6df930be7Sderaadt  * Redistribution and use in source and binary forms, with or without
7df930be7Sderaadt  * modification, are permitted provided that the following conditions
8df930be7Sderaadt  * are met:
9df930be7Sderaadt  * 1. Redistributions of source code must retain the above copyright
10df930be7Sderaadt  *    notice, this list of conditions and the following disclaimer.
11df930be7Sderaadt  * 2. Redistributions in binary form must reproduce the above copyright
12df930be7Sderaadt  *    notice, this list of conditions and the following disclaimer in the
13df930be7Sderaadt  *    documentation and/or other materials provided with the distribution.
146580fee3Smillert  * 3. Neither the name of the University nor the names of its contributors
15df930be7Sderaadt  *    may be used to endorse or promote products derived from this software
16df930be7Sderaadt  *    without specific prior written permission.
17df930be7Sderaadt  *
18df930be7Sderaadt  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19df930be7Sderaadt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20df930be7Sderaadt  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21df930be7Sderaadt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22df930be7Sderaadt  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23df930be7Sderaadt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24df930be7Sderaadt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25df930be7Sderaadt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26df930be7Sderaadt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27df930be7Sderaadt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28df930be7Sderaadt  * SUCH DAMAGE.
29df930be7Sderaadt  */
30df930be7Sderaadt 
31df930be7Sderaadt #include <sys/types.h>
32df930be7Sderaadt #include <stdlib.h>
33df930be7Sderaadt 
34d8bc04e4Spat static __inline char	*med3(char *, char *, char *, int (*)(const void *, const void *));
35b95a9429Sotto static __inline void	 swapfunc(char *, char *, size_t, int);
36df930be7Sderaadt 
37df930be7Sderaadt #define min(a, b)	(a) < (b) ? a : b
38df930be7Sderaadt 
39df930be7Sderaadt /*
40df930be7Sderaadt  * Qsort routine from Bentley & McIlroy's "Engineering a Sort Function".
414520aa4fSmillert  *
424520aa4fSmillert  * This version differs from Bentley & McIlroy in the following ways:
434520aa4fSmillert  *   1. The partition value is swapped into a[0] instead of being
444520aa4fSmillert  *	stored out of line.
454520aa4fSmillert  *
463ea23ff6Smillert  *   2. The swap function can swap 32-bit aligned elements on 64-bit
473ea23ff6Smillert  *	platforms instead of swapping them as byte-aligned.
483ea23ff6Smillert  *
493ea23ff6Smillert  *   3. It uses David Musser's introsort algorithm to fall back to
504520aa4fSmillert  *	heapsort(3) when the recursion depth reaches 2*lg(n + 1).
514520aa4fSmillert  *	This avoids quicksort's quadratic behavior for pathological
524520aa4fSmillert  *	input without appreciably changing the average run time.
534520aa4fSmillert  *
543ea23ff6Smillert  *   4. Tail recursion is eliminated when sorting the larger of two
554520aa4fSmillert  *	subpartitions to save stack space.
56df930be7Sderaadt  */
573ea23ff6Smillert #define SWAPTYPE_BYTEV	1
583ea23ff6Smillert #define SWAPTYPE_INTV	2
593ea23ff6Smillert #define SWAPTYPE_LONGV	3
603ea23ff6Smillert #define SWAPTYPE_INT	4
613ea23ff6Smillert #define SWAPTYPE_LONG	5
623ea23ff6Smillert 
633ea23ff6Smillert #define TYPE_ALIGNED(TYPE, a, es)			\
643ea23ff6Smillert 	(((char *)a - (char *)0) % sizeof(TYPE) == 0 && es % sizeof(TYPE) == 0)
653ea23ff6Smillert 
66df930be7Sderaadt #define swapcode(TYPE, parmi, parmj, n) { 		\
67b95a9429Sotto 	size_t i = (n) / sizeof (TYPE); 		\
68d8bc04e4Spat 	TYPE *pi = (TYPE *) (parmi); 			\
69d8bc04e4Spat 	TYPE *pj = (TYPE *) (parmj); 			\
70df930be7Sderaadt 	do { 						\
71d8bc04e4Spat 		TYPE	t = *pi;			\
72df930be7Sderaadt 		*pi++ = *pj;				\
73df930be7Sderaadt 		*pj++ = t;				\
74df930be7Sderaadt         } while (--i > 0);				\
75df930be7Sderaadt }
76df930be7Sderaadt 
77d6b09060Stholo static __inline void
swapfunc(char * a,char * b,size_t n,int swaptype)78b95a9429Sotto swapfunc(char *a, char *b, size_t n, int swaptype)
79df930be7Sderaadt {
803ea23ff6Smillert 	switch (swaptype) {
813ea23ff6Smillert 	case SWAPTYPE_INT:
823ea23ff6Smillert 	case SWAPTYPE_INTV:
833ea23ff6Smillert 		swapcode(int, a, b, n);
843ea23ff6Smillert 		break;
853ea23ff6Smillert 	case SWAPTYPE_LONG:
863ea23ff6Smillert 	case SWAPTYPE_LONGV:
873ea23ff6Smillert 		swapcode(long, a, b, n);
883ea23ff6Smillert 		break;
893ea23ff6Smillert 	default:
903ea23ff6Smillert 		swapcode(char, a, b, n);
913ea23ff6Smillert 		break;
923ea23ff6Smillert 	}
93df930be7Sderaadt }
94df930be7Sderaadt 
953ea23ff6Smillert #define swap(a, b)	do {				\
963ea23ff6Smillert 	switch (swaptype) {				\
973ea23ff6Smillert 	case SWAPTYPE_INT: {				\
983ea23ff6Smillert 		int t = *(int *)(a);			\
993ea23ff6Smillert 		*(int *)(a) = *(int *)(b);		\
1003ea23ff6Smillert 		*(int *)(b) = t;			\
1013ea23ff6Smillert 		break;					\
1023ea23ff6Smillert 	    }						\
1033ea23ff6Smillert 	case SWAPTYPE_LONG: {				\
104df930be7Sderaadt 		long t = *(long *)(a);			\
105df930be7Sderaadt 		*(long *)(a) = *(long *)(b);		\
106df930be7Sderaadt 		*(long *)(b) = t;			\
1073ea23ff6Smillert 		break;					\
1083ea23ff6Smillert 	    }						\
1093ea23ff6Smillert 	default:					\
1103ea23ff6Smillert 		swapfunc(a, b, es, swaptype);		\
1113ea23ff6Smillert 	}						\
1123ea23ff6Smillert } while (0)
113df930be7Sderaadt 
114df930be7Sderaadt #define vecswap(a, b, n) 	if ((n) > 0) swapfunc(a, b, n, swaptype)
115df930be7Sderaadt 
116d6b09060Stholo static __inline char *
med3(char * a,char * b,char * c,int (* cmp)(const void *,const void *))117d8bc04e4Spat med3(char *a, char *b, char *c, int (*cmp)(const void *, const void *))
118df930be7Sderaadt {
119df930be7Sderaadt 	return cmp(a, b) < 0 ?
120df930be7Sderaadt 	       (cmp(b, c) < 0 ? b : (cmp(a, c) < 0 ? c : a ))
121df930be7Sderaadt               :(cmp(b, c) > 0 ? b : (cmp(a, c) < 0 ? a : c ));
122df930be7Sderaadt }
123df930be7Sderaadt 
1244520aa4fSmillert static void
introsort(char * a,size_t n,size_t es,size_t maxdepth,int swaptype,int (* cmp)(const void *,const void *))1253ea23ff6Smillert introsort(char *a, size_t n, size_t es, size_t maxdepth, int swaptype,
1264520aa4fSmillert     int (*cmp)(const void *, const void *))
127df930be7Sderaadt {
128df930be7Sderaadt 	char *pa, *pb, *pc, *pd, *pl, *pm, *pn;
1293ea23ff6Smillert 	int cmp_result;
1304520aa4fSmillert 	size_t r, s;
131df930be7Sderaadt 
132*7d863ca8Smillert loop:	if (n < 7) {
133822266edSmillert 		for (pm = a + es; pm < a + n * es; pm += es)
134822266edSmillert 			for (pl = pm; pl > a && cmp(pl - es, pl) > 0;
135df930be7Sderaadt 			     pl -= es)
136df930be7Sderaadt 				swap(pl, pl - es);
137df930be7Sderaadt 		return;
138df930be7Sderaadt 	}
139*7d863ca8Smillert 	if (maxdepth == 0) {
140*7d863ca8Smillert 		if (heapsort(a, n, es, cmp) == 0)
141*7d863ca8Smillert 			return;
142*7d863ca8Smillert 	}
143*7d863ca8Smillert 	maxdepth--;
144822266edSmillert 	pm = a + (n / 2) * es;
145df930be7Sderaadt 	if (n > 7) {
146822266edSmillert 		pl = a;
147822266edSmillert 		pn = a + (n - 1) * es;
148df930be7Sderaadt 		if (n > 40) {
1494520aa4fSmillert 			s = (n / 8) * es;
1504520aa4fSmillert 			pl = med3(pl, pl + s, pl + 2 * s, cmp);
1514520aa4fSmillert 			pm = med3(pm - s, pm, pm + s, cmp);
1524520aa4fSmillert 			pn = med3(pn - 2 * s, pn - s, pn, cmp);
153df930be7Sderaadt 		}
154df930be7Sderaadt 		pm = med3(pl, pm, pn, cmp);
155df930be7Sderaadt 	}
156df930be7Sderaadt 	swap(a, pm);
157822266edSmillert 	pa = pb = a + es;
158822266edSmillert 	pc = pd = a + (n - 1) * es;
159df930be7Sderaadt 	for (;;) {
160b95a9429Sotto 		while (pb <= pc && (cmp_result = cmp(pb, a)) <= 0) {
161b95a9429Sotto 			if (cmp_result == 0) {
162df930be7Sderaadt 				swap(pa, pb);
163df930be7Sderaadt 				pa += es;
164df930be7Sderaadt 			}
165df930be7Sderaadt 			pb += es;
166df930be7Sderaadt 		}
167b95a9429Sotto 		while (pb <= pc && (cmp_result = cmp(pc, a)) >= 0) {
168b95a9429Sotto 			if (cmp_result == 0) {
169df930be7Sderaadt 				swap(pc, pd);
170df930be7Sderaadt 				pd -= es;
171df930be7Sderaadt 			}
172df930be7Sderaadt 			pc -= es;
173df930be7Sderaadt 		}
174df930be7Sderaadt 		if (pb > pc)
175df930be7Sderaadt 			break;
176df930be7Sderaadt 		swap(pb, pc);
177df930be7Sderaadt 		pb += es;
178df930be7Sderaadt 		pc -= es;
179df930be7Sderaadt 	}
180df930be7Sderaadt 
181822266edSmillert 	pn = a + n * es;
182822266edSmillert 	r = min(pa - a, pb - pa);
183df930be7Sderaadt 	vecswap(a, pb - r, r);
184df930be7Sderaadt 	r = min(pd - pc, pn - pd - es);
185df930be7Sderaadt 	vecswap(pb, pn - r, r);
1862c0d6f28Smillert 	/*
1872c0d6f28Smillert 	 * To save stack space we sort the smaller side of the partition first
1882c0d6f28Smillert 	 * using recursion and eliminate tail recursion for the larger side.
1892c0d6f28Smillert 	 */
1902c0d6f28Smillert 	r = pb - pa;
1912c0d6f28Smillert 	s = pd - pc;
1922c0d6f28Smillert 	if (r < s) {
1932c0d6f28Smillert 		/* Recurse for 1st side, iterate for 2nd side. */
1942c0d6f28Smillert 		if (s > es) {
1953ea23ff6Smillert 			if (r > es) {
1963ea23ff6Smillert 				introsort(a, r / es, es, maxdepth,
1973ea23ff6Smillert 				    swaptype, cmp);
1983ea23ff6Smillert 			}
1992c0d6f28Smillert 			a = pn - s;
2002c0d6f28Smillert 			n = s / es;
2012c0d6f28Smillert 			goto loop;
2022c0d6f28Smillert 		}
2032c0d6f28Smillert 	} else {
2042c0d6f28Smillert 		/* Recurse for 2nd side, iterate for 1st side. */
2052c0d6f28Smillert 		if (r > es) {
2063ea23ff6Smillert 			if (s > es) {
2073ea23ff6Smillert 				introsort(pn - s, s / es, es, maxdepth,
2083ea23ff6Smillert 				    swaptype, cmp);
2093ea23ff6Smillert 			}
210df930be7Sderaadt 			n = r / es;
211df930be7Sderaadt 			goto loop;
212df930be7Sderaadt 		}
2132c0d6f28Smillert 	}
214df930be7Sderaadt }
2154520aa4fSmillert 
2164520aa4fSmillert void
qsort(void * a,size_t n,size_t es,int (* cmp)(const void *,const void *))2174520aa4fSmillert qsort(void *a, size_t n, size_t es, int (*cmp)(const void *, const void *))
2184520aa4fSmillert {
2194520aa4fSmillert 	size_t i, maxdepth = 0;
2203ea23ff6Smillert 	int swaptype;
2214520aa4fSmillert 
2224520aa4fSmillert 	/* Approximate 2*ceil(lg(n + 1)) */
2234520aa4fSmillert 	for (i = n; i > 0; i >>= 1)
2244520aa4fSmillert 		maxdepth++;
2254520aa4fSmillert 	maxdepth *= 2;
2264520aa4fSmillert 
2273ea23ff6Smillert 	if (TYPE_ALIGNED(long, a, es))
2283ea23ff6Smillert 		swaptype = es == sizeof(long) ? SWAPTYPE_LONG : SWAPTYPE_LONGV;
2293ea23ff6Smillert 	else if (sizeof(int) != sizeof(long) && TYPE_ALIGNED(int, a, es))
2303ea23ff6Smillert 		swaptype = es == sizeof(int) ? SWAPTYPE_INT : SWAPTYPE_INTV;
2313ea23ff6Smillert 	else
2323ea23ff6Smillert 		swaptype = SWAPTYPE_BYTEV;
2333ea23ff6Smillert 
2343ea23ff6Smillert 	introsort(a, n, es, maxdepth, swaptype, cmp);
2353ea23ff6Smillert 
2364520aa4fSmillert }
2374520aa4fSmillert 
2380d943ef0Sguenther DEF_STRONG(qsort);
239