javascriptnumpyquickselect

numpy.partition in JavaScript


Is there a readily available equivalent to numpy.partition in JavaScript, via a library or built in?

It does not seem like any of the popular relevant libraries such as underscore.js provide such a function. I am asking because I would like to be able to find the n highest (or lowest) elements in an array for the general case, without having to implement quickselect or introselect myself.

Partitioning an array at index n rearranges the array so that the element at n is in sorted order, and all the elements with index greater than n are larger than that element at n. Alternatively, the elements at indices less than n can all be less than the element at n. Either way, it is a partial sort that guarantees the position of a particular element and the distribution of the elements above and below it.

A full sort satisfies the same conditions of course, but runs in O(n log n) time, while partitioning generally runs in O(n) time (average case for quickselect, worst case for introselect).

The jQuery plugin QuickSelect does something completely different, despite the promising name.

Part of the motivation for this question: Possible to use Math.min to get second smallest number from array?


Solution

  • Package to do quickselect.

    Github

    NPM

    (I've never used this package, but from the README, it seems to be what OP is looking for)