summaryrefslogtreecommitdiff
path: root/libc/stdlib/qsort.c
blob: 7cb1d8ab4e3f166023f30f321a3cfbecf1c47112 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
 * This file lifted in toto from 'Dlibs' on the atari ST  (RdeBath)
 *
 * 
 *    Dale Schumacher                         399 Beacon Ave.
 *    (alias: Dalnefre')                      St. Paul, MN  55104
 *    dal@syntel.UUCP                         United States of America
 *  "It's not reality that's important, but how you perceive things."
 */
#include <string.h>

char *_qbuf = 0;				/* pointer to storage for qsort() */

#define	PIVOT			((i+j)>>1)
#define moveitem(dst,src,size)	if(dst != src) memcpy(dst, src, size)

static void _wqsort(base, lo, hi, cmp)
register int *base;
register int lo;
register int hi;
register int (*cmp) ();
{
	int k;
	register int i, j, t;
	register int *p = &k;

	while (hi > lo) {
		i = lo;
		j = hi;
		t = PIVOT;
		*p = base[t];
		base[t] = base[i];
		base[i] = *p;
		while (i < j) {
			while (((*cmp) ((base + j), p)) > 0)
				--j;
			base[i] = base[j];
			while ((i < j) && (((*cmp) ((base + i), p)) <= 0))
				++i;
			base[j] = base[i];
		}
		base[i] = *p;
		if ((i - lo) < (hi - i)) {
			_wqsort(base, lo, (i - 1), cmp);
			lo = i + 1;
		} else {
			_wqsort(base, (i + 1), hi, cmp);
			hi = i - 1;
		}
	}
}

static void _lqsort(base, lo, hi, cmp)
register long *base;
register int lo;
register int hi;
register int (*cmp) ();
{
	long k;
	register int i, j, t;
	register long *p = &k;

	while (hi > lo) {
		i = lo;
		j = hi;
		t = PIVOT;
		*p = base[t];
		base[t] = base[i];
		base[i] = *p;
		while (i < j) {
			while (((*cmp) ((base + j), p)) > 0)
				--j;
			base[i] = base[j];
			while ((i < j) && (((*cmp) ((base + i), p)) <= 0))
				++i;
			base[j] = base[i];
		}
		base[i] = *p;
		if ((i - lo) < (hi - i)) {
			_lqsort(base, lo, (i - 1), cmp);
			lo = i + 1;
		} else {
			_lqsort(base, (i + 1), hi, cmp);
			hi = i - 1;
		}
	}
}

static void _nqsort(base, lo, hi, size, cmp)
register char *base;
register int lo;
register int hi;
register int size;
register int (*cmp) ();
{
	register int i, j;
	register char *p = _qbuf;

	while (hi > lo) {
		i = lo;
		j = hi;
		p = (base + size * PIVOT);
		moveitem(_qbuf, p, size);
		moveitem(p, (base + size * i), size);
		moveitem((base + size * i), _qbuf, size);
		p = _qbuf;
		while (i < j) {
			while (((*cmp) ((base + size * j), p)) > 0)
				--j;
			moveitem((base + size * i), (base + size * j), size);
			while ((i < j) && (((*cmp) ((base + size * i), p)) <= 0))
				++i;
			moveitem((base + size * j), (base + size * i), size);
		}
		moveitem((base + size * i), p, size);
		if ((i - lo) < (hi - i)) {
			_nqsort(base, lo, (i - 1), size, cmp);
			lo = i + 1;
		} else {
			_nqsort(base, (i + 1), hi, size, cmp);
			hi = i - 1;
		}
	}
}

extern int qsort(base, num, size, cmp)
char *base;
int num;
int size;
int (*cmp) ();
{
	char _qtemp[128];

	if (_qbuf == 0) {
		if (size > sizeof(_qtemp))	/* records too large! */
			return 1;
		_qbuf = _qtemp;
	}
	if (size == 2)
		_wqsort(base, 0, num - 1, cmp);
	else if (size == 4)
		_lqsort(base, 0, num - 1, cmp);
	else
		_nqsort(base, 0, num - 1, size, cmp);
	if (_qbuf == _qtemp)
		_qbuf = 0;
	return 0;
}