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
| function qsort_3part(a, l, r) {
if (r <= l) return;
var i = l, j = r+1, p = l, q = r+1;
var v = a[l];
while (i < j) {
while (a[++i] < v);
while (v < a[--j]);
if (i < j) {
exch(a, i, j);
if (a[i] === v) {
p++;
exch(a, p, i);
}
if (a[j] === v) {
q--;
exch(a, q, j);
}
}
}
exch(a, j, l);
i = j-1;
j = j+1;
for (var k = l+1; k <= p; k++, i--) exch(a, k, i);
for (var k = r; k >= q; k--, j++) exch(a, k, j);
qsort_3part(a, l, i);
qsort_3part(a, j, r);
}
|