Hey everyone, I’m wiring up a leaderboard helper and trying to avoid a full sort because the list can get pretty big, but my quickselect version occasionally returns the wrong item after a few swaps and that makes me nervous about using it in production.
function quickselect(arr, k, left = 0, right = arr.length - 1) {
if (left === right) return arr[left];
const pivot = arr[right];
let i = left;
for (let j = left; j < right; j++) {
if (arr[j] <= pivot) {
[arr[i], arr[j]] = [arr[j], arr[i]];
i++;
}
}
[arr[i], arr[right]] = [arr[right], arr[i]];
if (k === i) return arr[i];
if (k < i) return quickselect(arr, k, left, i - 1);
return quickselect(arr, k, i + 1, right);
}
console.log(quickselect([9, 1, 4, 7, 3, 8, 2], 3));
What am I getting wrong in the partition logic here, especially when I want speed without quietly returning a bad rank?
Hari