-
Notifications
You must be signed in to change notification settings - Fork 2
/
candidates.jl
74 lines (71 loc) · 1.79 KB
/
candidates.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
const SMALL_THRESHOLD = 20
# insertion sort
function isort(v, lo, hi)
@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
# updated qsort
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 isless(v[mi], v[lo])
v[lo], v[mi] = v[mi], v[lo];
end
if isless(v[hi], v[mi])
v[mi], v[hi] = v[hi], v[mi];
end
if isless(v[mi], v[lo])
v[mi], v[lo] = v[lo], v[mi];
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];
lo < (j - 1) && qsort_c_mp!(v, lo, j-1);
lo = j+1
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