xref: /openbsd-src/lib/libc/stdlib/qsort.c (revision c5f4fad510dd427c0c20c0f4d164f60ce24651b6)
1 /*-
2  * Copyright (c) 1992, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #if defined(LIBC_SCCS) && !defined(lint)
31 static char *rcsid = "$OpenBSD: qsort.c,v 1.9 2005/03/30 18:51:49 pat Exp $";
32 #endif /* LIBC_SCCS and not lint */
33 
34 #include <sys/types.h>
35 #include <stdlib.h>
36 
37 static __inline char	*med3(char *, char *, char *, int (*)(const void *, const void *));
38 static __inline void	 swapfunc(char *, char *, int, int);
39 
40 #define min(a, b)	(a) < (b) ? a : b
41 
42 /*
43  * Qsort routine from Bentley & McIlroy's "Engineering a Sort Function".
44  */
45 #define swapcode(TYPE, parmi, parmj, n) { 		\
46 	long i = (n) / sizeof (TYPE); 			\
47 	TYPE *pi = (TYPE *) (parmi); 			\
48 	TYPE *pj = (TYPE *) (parmj); 			\
49 	do { 						\
50 		TYPE	t = *pi;			\
51 		*pi++ = *pj;				\
52 		*pj++ = t;				\
53         } while (--i > 0);				\
54 }
55 
56 #define SWAPINIT(a, es) swaptype = ((char *)a - (char *)0) % sizeof(long) || \
57 	es % sizeof(long) ? 2 : es == sizeof(long)? 0 : 1;
58 
59 static __inline void
60 swapfunc(char *a, char *b, int n, int swaptype)
61 {
62 	if (swaptype <= 1)
63 		swapcode(long, a, b, n)
64 	else
65 		swapcode(char, a, b, n)
66 }
67 
68 #define swap(a, b)					\
69 	if (swaptype == 0) {				\
70 		long t = *(long *)(a);			\
71 		*(long *)(a) = *(long *)(b);		\
72 		*(long *)(b) = t;			\
73 	} else						\
74 		swapfunc(a, b, es, swaptype)
75 
76 #define vecswap(a, b, n) 	if ((n) > 0) swapfunc(a, b, n, swaptype)
77 
78 static __inline char *
79 med3(char *a, char *b, char *c, int (*cmp)(const void *, const void *))
80 {
81 	return cmp(a, b) < 0 ?
82 	       (cmp(b, c) < 0 ? b : (cmp(a, c) < 0 ? c : a ))
83               :(cmp(b, c) > 0 ? b : (cmp(a, c) < 0 ? a : c ));
84 }
85 
86 void
87 qsort(void *aa, size_t n, size_t es, int (*cmp)(const void *, const void *))
88 {
89 	char *pa, *pb, *pc, *pd, *pl, *pm, *pn;
90 	int d, r, swaptype, swap_cnt;
91 	char *a = aa;
92 
93 loop:	SWAPINIT(a, es);
94 	swap_cnt = 0;
95 	if (n < 7) {
96 		for (pm = (char *)a + es; pm < (char *) a + n * es; pm += es)
97 			for (pl = pm; pl > (char *) a && cmp(pl - es, pl) > 0;
98 			     pl -= es)
99 				swap(pl, pl - es);
100 		return;
101 	}
102 	pm = (char *)a + (n / 2) * es;
103 	if (n > 7) {
104 		pl = (char *)a;
105 		pn = (char *)a + (n - 1) * es;
106 		if (n > 40) {
107 			d = (n / 8) * es;
108 			pl = med3(pl, pl + d, pl + 2 * d, cmp);
109 			pm = med3(pm - d, pm, pm + d, cmp);
110 			pn = med3(pn - 2 * d, pn - d, pn, cmp);
111 		}
112 		pm = med3(pl, pm, pn, cmp);
113 	}
114 	swap(a, pm);
115 	pa = pb = (char *)a + es;
116 
117 	pc = pd = (char *)a + (n - 1) * es;
118 	for (;;) {
119 		while (pb <= pc && (r = cmp(pb, a)) <= 0) {
120 			if (r == 0) {
121 				swap_cnt = 1;
122 				swap(pa, pb);
123 				pa += es;
124 			}
125 			pb += es;
126 		}
127 		while (pb <= pc && (r = cmp(pc, a)) >= 0) {
128 			if (r == 0) {
129 				swap_cnt = 1;
130 				swap(pc, pd);
131 				pd -= es;
132 			}
133 			pc -= es;
134 		}
135 		if (pb > pc)
136 			break;
137 		swap(pb, pc);
138 		swap_cnt = 1;
139 		pb += es;
140 		pc -= es;
141 	}
142 	if (swap_cnt == 0) {  /* Switch to insertion sort */
143 		for (pm = (char *) a + es; pm < (char *) a + n * es; pm += es)
144 			for (pl = pm; pl > (char *) a && cmp(pl - es, pl) > 0;
145 			     pl -= es)
146 				swap(pl, pl - es);
147 		return;
148 	}
149 
150 	pn = (char *)a + n * es;
151 	r = min(pa - (char *)a, pb - pa);
152 	vecswap(a, pb - r, r);
153 	r = min(pd - pc, pn - pd - es);
154 	vecswap(pb, pn - r, r);
155 	if ((r = pb - pa) > es)
156 		qsort(a, r / es, es, cmp);
157 	if ((r = pd - pc) > es) {
158 		/* Iterate rather than recurse to save stack space */
159 		a = pn - r;
160 		n = r / es;
161 		goto loop;
162 	}
163 /*		qsort(pn - r, r / es, es, cmp);*/
164 }
165