Is there an equivalent available numpy.partition
in JavaScript through the library or inline?
It seems that not one of the popular libraries like underscore.js provides such a function. I ask because I would like to find the n
highest (or lowest) elements in the array for the general case, without having to implement quickselect or introselect myself.
Dividing the array by index n
rebuilds the array so that the at element is n
in sorted order, and all elements with an index are n
larger than this element in n
. Alternatively, items with indexes n
may be less than item c n
. In any case, this is a partial view that guarantees the position of a particular element and the distribution of elements above and below.
Full sorting, of course, satisfies the same conditions, but works in O(n log n)
time, while separation is usually done in O(n)
time (average case for quick selection, worst case for introselect).
The jQuery QuickSelect plugin does something completely different, despite its promising name.
Part of the motivation for this question: Can I use Math.min to get the second smallest number from an array?
source
share