Security News
ESLint is Now Language-Agnostic: Linting JSON, Markdown, and Beyond
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
quickselect
Advanced tools
The quickselect npm package is a fast selection algorithm implementation for JavaScript, based on Tony Hoare's quickselect algorithm. It allows for efficiently selecting the k-th smallest element from an unsorted list without fully sorting the array. This can be particularly useful in applications where partial sorting is sufficient, such as finding the median or other order statistics in a dataset.
Selecting the k-th smallest element
This feature allows you to rearrange the array so that the element at the k-th position is the one that would be in that position if the array were sorted. The elements before it are smaller, and the elements after it are larger. This is useful for finding specific order statistics, such as the median, in linear time.
const quickselect = require('quickselect');
let arr = [5, 3, 7, 6, 2, 9];
quickselect(arr, 2);
console.log(arr[2]); // Outputs: 5
The 'heap' package provides a way to create min and max heaps in JavaScript. While not directly offering the same functionality as quickselect, heaps can be used to efficiently find the k-th smallest or largest element by inserting elements into the heap and then extracting the minimum or maximum k times. This approach is more suited for streaming data or when multiple order statistics need to be found over time.
The 'simple-statistics' package offers a wide range of statistical functions, including a method for finding the median. While it does not provide a direct implementation of the quickselect algorithm, it uses sorting and other techniques to calculate various statistics, which can be useful in data analysis scenarios where more than just order statistics are needed.
A tiny and fast selection algorithm in JavaScript (specifically, Floyd-Rivest selection).
quickselect(array, k[, left, right, compareFn]);
Rearranges items so that all items in the [left, k]
are the smallest.
The k
-th element will have the (k - left + 1)
-th smallest value in [left, right]
.
array
: the array to partially sort (in place)k
: middle index for partial sorting (as defined above)left
: left index of the range to sort (0
by default)right
: right index (last index of the array by default)compareFn
: compare functionExample:
var arr = [65, 28, 59, 33, 21, 56, 22, 95, 50, 12, 90, 53, 28, 77, 39];
quickselect(arr, 8);
// arr is [39, 28, 28, 33, 21, 12, 22, 50, 53, 56, 59, 65, 90, 77, 95]
// ^^ middle index
FAQs
A tiny and fast selection algorithm in JavaScript.
The npm package quickselect receives a total of 2,455,452 weekly downloads. As such, quickselect popularity was classified as popular.
We found that quickselect demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
Security News
Members Hub is conducting large-scale campaigns to artificially boost Discord server metrics, undermining community trust and platform integrity.
Security News
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.