Quickselect

3.0.0 · active · verified Sun Apr 19

quickselect is a highly optimized, tiny JavaScript library implementing the Floyd-Rivest selection algorithm. It efficiently partially sorts an array in-place, ensuring that elements up to a specified index `k` are the smallest in the given range. The current stable version is 3.0.0, which was a significant update dropping CommonJS and UMD builds, making it exclusively ESM and requiring modern ES syntax. Releases appear to be infrequent, driven by significant architectural changes or feature additions. Its primary differentiator is its minimal footprint and speed for selection tasks compared to full sorting algorithms, making it ideal for scenarios where only a subset of sorted elements is needed.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to use `quickselect` to partially sort an array in-place, ensuring that elements up to a specific index `k` are the smallest in the given range.

import quickselect from 'quickselect';

const arr = [65, 28, 59, 33, 21, 56, 22, 95, 50, 12, 90, 53, 28, 77, 39];

// Rearrange the array such that elements up to index 8 are the smallest.
// The 8th element (0-indexed) will be the (8 - 0 + 1)th smallest value in the original array.
quickselect(arr, 8);

console.log(arr);
// Expected output similar to: [39, 28, 28, 33, 21, 12, 22, 50, 53, 56, 59, 65, 90, 77, 95]
// Note: The order of elements *before* k is not guaranteed, only that they are smaller than or equal to arr[k].

view raw JSON →