-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsedgesort.c
211 lines (194 loc) · 6.01 KB
/
sedgesort.c
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
* A library of sorting functions
*
* Written by: Ariel Faigon, 1987
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details (to receive a copy of the GNU
* General Public License, write to the Free Software Foundation, Inc., 675
* Mass Ave, Cambridge, MA 02139, USA).
*
*/
#include <stdio.h>
#include "sort.h"
#ifdef __APPLE__
void insort_long (long *array, int len);
void insort_float (float *array, int len);
void insort_double (double *array, int len);
void insort_short (short *array, int len);
#endif
/*-------------------------------------------------------------------
* This file shouldn't be touched.
* For customizable parameters, see 'sort.h'
*-----------------------------------------------------------------*/
/* 15 has been found empirically as the optimal cutoff value */
#ifndef CUTOFF
# define CUTOFF 15
#endif
/*
| void partial_quickersort (array, lower, upper)
| KEY_T array[];
| int lower, upper;
|
| Abstract:
| Sort array[lower..upper] into a partial order
| leaving segments which are CUTOFF elements long
| unsorted internally.
|
| Efficiency:
| Could be made faster for _worst_ cases by selecting
| a pivot using median-of-3. I don't do it because
| in practical cases my pivot selection is arbitrary and
| thus pretty random, your mileage may vary.
|
| Method:
| Partial Quicker-sort using a sentinel (ala Robert Sedgewick)
|
| BIG NOTE:
| Precondition: array[upper+1] holds the maximum possible key.
| with a cutoff value of CUTOFF.
*/
void partial_quickersort_long (long *array, int lower, int upper)
{
int i, j;
long temp, pivot;
if (upper - lower > CUTOFF) {
SWAP(array[lower], array[(upper+lower)/2]);
i = lower; j = upper + 1; pivot = array[lower];
while (1) {
/*
* ------------------------- NOTE --------------------------
* ignoring BIG NOTE above may lead to an infinite loop here
* ---------------------------------------------------------
*/
do i++; while (LT(array[i], pivot));
do j--; while (GT(array[j], pivot));
if (j < i) break;
SWAP(array[i], array[j]);
}
SWAP(array[lower], array[j]);
partial_quickersort_long (array, lower, j - 1);
partial_quickersort_long (array, i, upper);
}
}
void partial_quickersort_float (array, lower, upper)
register float array[];
register int lower, upper;
{
register int i, j;
register float temp, pivot;
if (upper - lower > CUTOFF) {
SWAP(array[lower], array[(upper+lower)/2]);
i = lower; j = upper + 1; pivot = array[lower];
while (1) {
do i++; while (LT(array[i], pivot));
do j--; while (GT(array[j], pivot));
if (j < i) break;
SWAP(array[i], array[j]);
}
SWAP(array[lower], array[j]);
partial_quickersort_float (array, lower, j - 1);
partial_quickersort_float (array, i, upper);
}
}
void partial_quickersort_double (array, lower, upper)
register double array[];
register int lower, upper;
{
register int i, j;
register double temp, pivot;
if (upper - lower > CUTOFF) {
SWAP(array[lower], array[(upper+lower)/2]);
i = lower; j = upper + 1; pivot = array[lower];
while (1) {
do i++; while (LT(array[i], pivot));
do j--; while (GT(array[j], pivot));
if (j < i) break;
SWAP(array[i], array[j]);
}
SWAP(array[lower], array[j]);
partial_quickersort_double (array, lower, j - 1);
partial_quickersort_double (array, i, upper);
}
}
void partial_quickersort_short (array, lower, upper)
register short array[];
register int lower, upper;
{
register int i, j;
register short temp, pivot;
if (upper - lower > CUTOFF) {
SWAP(array[lower], array[(upper+lower)/2]);
i = lower; j = upper + 1; pivot = array[lower];
while (1) {
do i++; while (LT(array[i], pivot));
do j--; while (GT(array[j], pivot));
if (j < i) break;
SWAP(array[i], array[j]);
}
SWAP(array[lower], array[j]);
partial_quickersort_short (array, lower, j - 1);
partial_quickersort_short (array, i, upper);
}
}
/*
| void _sedgesort (array, len)
| KEY_T array[];
| int len;
|
| Abstract:
| Sort array[0..len-1] into increasing order.
|
| Method:
| Use partial_quickersort() with a sentinel (ala Sedgewick)
| to reach a partial order, leave the unsorted segments of
| length == CUTOFF to a simpler low-overhead, insertion sort.
|
| This method seems to me the ultimative sort method in terms
| of average efficiency (Skeptic ? try to beat it).
|
| BIG NOTE:
| precondition: array[len] must hold a sentinel (largest
| possible value) in order for this to work correctly.
| An easy way to do this is to declare an array that has
| len+1 elements [0..len], and assign MAXINT or some such
| to the last location before starting the sort (see sorttest.c)
*/
void _sedgesort_long (long *array, int len)
{
/*
* ------------------------- NOTE --------------------------
* ignoring BIG NOTE above may lead to an infinite loop here
* ---------------------------------------------------------
*/
partial_quickersort_long (array, 0, len - 1);
insort_long (array, len);
}
void _sedgesort_float (array, len)
register float array[];
register int len;
{
partial_quickersort_float (array, 0, len - 1);
insort_float (array, len);
}
void _sedgesort_double (array, len)
register double array[];
register int len;
{
partial_quickersort_double (array, 0, len - 1);
insort_double (array, len);
}
void _sedgesort_short (array, len)
register short array[];
register int len;
{
partial_quickersort_short (array, 0, len - 1);
insort_short (array, len);
}