Skip to content

Commit 4db4e4f

Browse files
committed
fix pivot selection
1 parent 3a6f0f0 commit 4db4e4f

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

index.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,22 @@ function upperBound(value, arr) {
343343
function sort(values, boxes, indices, left, right, nodeSize) {
344344
if (Math.floor(left / nodeSize) >= Math.floor(right / nodeSize)) return;
345345

346-
const pivot = values[(left + right) >> 1];
346+
// apply median of three method
347+
const start = values[left];
348+
const mid = values[(left + right) >> 1];
349+
const end = values[right];
350+
351+
let pivot = end;
352+
353+
const x = Math.max(start, mid);
354+
if (end > x) {
355+
pivot = x;
356+
} else if (x === start) {
357+
pivot = Math.max(mid, end);
358+
} else if (x === mid) {
359+
pivot = Math.max(start, end);
360+
}
361+
347362
let i = left - 1;
348363
let j = right + 1;
349364

0 commit comments

Comments
 (0)