-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathqsorts.jl
275 lines (263 loc) · 6.73 KB
/
qsorts.jl
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
const SMALL_THRESHOLD = 20
# insertion sort
function isort(v, lo=1, hi=length(v))
@inbounds for i = lo+1:hi
j = i
x = v[i]
while j > lo
if isless(x, v[j-1])
v[j] = v[j-1]
j -= 1
continue
end
break
end
v[j] = x
end
return v;
end
# fisher-yates shuffle
function fyshuffle(v)
@inbounds begin
N = length(v);
for i = length(v):0
j = rand(1:N-1);
v[i], v[j] = v[j], v[i];
N -= 1;
end
end
return v;
end
# canonical quicksort + fisher-yates shuffle
function qsort_c_fy!(v, lo=1, hi=length(v))
fyshuffle(v)
qsort_c!(v)
end
# ibid + median of 3 pivot
function qsort_c_fy_mp!(v, lo=1, hi=length(v))
fyshuffle(v)
qsort_c_mp!(v)
end
# median of 3 pivot
function qsort_c_mp!(v, lo=1, hi=length(v))
@inbounds while lo < hi
hi-lo <= SMALL_THRESHOLD && return isort(v, lo, hi)
mi = (lo+hi)>>>1
if v[lo] > v[mi]
v[lo], v[mi] = v[mi], v[lo];
end
if v[lo] > v[hi]
v[lo], v[hi] = v[hi], v[lo];
end
if v[mi] > v[lo]
v[mi], v[lo] = v[lo], v[mi];
end
v[mi], v[lo] = v[lo], v[mi]
highval = v[hi]
v[hi] = v[lo]
i, j = lo, hi;
pivot = v[lo]
while true;
i += 1;
while isless(v[i], pivot);
i += 1;
end
j -= 1;
while isless(pivot, v[j]);
j -= 1;
end
i >= j && break;
v[i], v[j] = v[j], v[i];
end
v[j], v[lo] = v[lo], v[j];
v[hi] = highval
qsort_c_mp!(v, lo, j-1);
lo = j+1
end
return v;
end
# median of 3 pivot
function qsort_s_mp!(v, lo=1, hi=length(v))
@inbounds while lo < hi
hi-lo <= SMALL_THRESHOLD && return isort(v, lo, hi)
mi = (lo+hi)>>>1
if v[lo] > v[mi]
if v[lo] < v[hi]
pivot = v[lo]
elseif v[mi] > v[hi]
pivot = v[mi]
else
pivot = v[hi]
end
else
if v[lo] > v[hi]
pivot = v[lo]
elseif v[mi] < v[hi]
pivot = v[mi]
else
pivot = v[hi]
end
end
i, j = lo, hi
while true;
while isless(v[i], pivot); i += 1; end
while isless(pivot, v[j]); j -= 1; end
i <= j || break;
v[i], v[j] = v[j], v[i]
i += 1; j -= 1;
end
lo < j && qsort_s_mp!(v, lo, j)
lo = i
end
return v;
end
# random pivot
function qsort_c_rp!(v, lo=1, hi=length(v))
@inbounds begin
hi <= lo && return;
hi-lo <= SMALL_THRESHOLD && return isort(v, lo, hi)
i, j = lo, hi+1;
rp = rand(lo:hi)
v[lo], v[rp] = v[rp], v[lo]
pivot = v[lo]
while true;
i += 1;
while isless(v[i], pivot);
i == hi && break;
i += 1;
end
j -= 1;
while isless(pivot, v[j]);
j -= 1;
end
i >= j && break;
v[i], v[j] = v[j], v[i]
end
v[lo], v[j] = v[j], v[lo]
qsort_c!(v, lo, j-1);
qsort_c!(v, j+1, hi);
return v;
end
end
# canonical, @inbounds macro
function qsort_c!(v, lo=1, hi=length(v))
@inbounds begin
hi <= lo && return;
hi-lo <= SMALL_THRESHOLD && return isort(v, lo, hi)
i, j = lo, hi+1;
mid = (lo+hi)>>>1
v[lo], v[mid] = v[mid], v[lo]
pivot = v[lo]
while true;
i += 1;
while isless(v[i], pivot);
i == hi && break;
i += 1;
end
j -= 1;
while isless(pivot, v[j]);
j -= 1;
end
i >= j && break;
v[i], v[j] = v[j], v[i]
end
v[lo], v[j] = v[j], v[lo]
qsort_c!(v, lo, j-1);
qsort_c!(v, j+1, hi);
end
return v;
end
# 3way quicksort
function qsort_3way!(v, lo=1, hi=length(v))
@inbounds begin
hi <= lo && return;
hi-lo <= SMALL_THRESHOLD && return isort(v, lo, hi)
lt = lo; gt = hi; i = lo;
piv = v[lo];
while i <= gt
elem = v[i];
if isless(elem, piv)
v[i], v[lt] = v[lt], v[i];
lt+=1; i+=1;
elseif isless(piv, elem)
v[i], v[gt] = v[gt], v[i];
gt-=1;
else i+=1; end
end
qsort_3way!(v, lo, lt-1)
qsort_3way!(v, gt+1, hi)
end
return v;
end
# julia's current implementation as of v0.2
function qsort_stdlib!(v, lo=1, hi=length(v))
@inbounds while lo < hi
hi-lo <= SMALL_THRESHOLD && return isort(v, lo, hi)
pivot = v[(lo+hi)>>>1]
i, j = lo, hi
while true;
while isless(v[i], pivot); i += 1; end
while isless(pivot, v[j]); j -= 1; end
i <= j || break;
v[i], v[j] = v[j], v[i]
i += 1; j -= 1;
end
lo < j && qsort_stdlib!(v, lo, j)
lo = i
end
return v;
end
# median of 3 pivot with the suggested change to median mechanics
# at most 4 assignments and 3 comparisons
function qsort_s_mp!(v, lo=1, hi=length(v))
@inbounds while lo < hi
hi-lo <= SMALL_THRESHOLD && return isort(v, lo, hi)
mi = (lo+hi)>>>1
if isless(v[hi], v[mi])
if isless(v[lo], v[hi])
mival = v[mi]
v[mi] = v[hi]
v[hi] = mival
else
loval = v[lo]
if isless(v[mi], v[lo])
v[lo] = v[hi]
v[hi] = loval
else
v[lo] = v[hi]
v[hi] = v[mi]
v[mi] = loval
end
end
elseif isless(v[mi], v[lo])
loval = v[lo]
if isless(v[lo], v[hi])
v[lo] = v[mi]
v[mi] = loval
else
v[lo] = v[mi]
v[mi] = v[hi]
v[hi] = loval
end
end
v[mi], v[lo] = v[lo], v[mi]
i, j = lo, hi;
pivot = v[lo]
while true;
i += 1;
while isless(v[i], pivot);
i += 1;
end
j -= 1;
while isless(pivot, v[j]);
j -= 1;
end
i >= j && break;
v[i], v[j] = v[j], v[i];
end
v[j], v[lo] = v[lo], v[j];
qsort_c_mp!(v, lo, j-1);
lo = j+1
end
return v;
end