kth-smallest
v2.2.7
Published
Find kth smallest element in an array of numbers in amortized linear time
Downloads
19
Readme
kth-smallest
Find kth smallest element in an arary of numbers
Uses a randomized quickselect to achieve O(n) average case time complexity
Install
$ npm install kth-smallest
Usage
import kthSmallest from 'kth-smallest'
kthSmallest([10, 20, 30, 40], 0)
// 10
kthSmallest([10, 20, 30, 40], 2)
// 30
kthSmallest([10, 20, 30, 40], -1)
// 40
API
kthSmallest :: number a => [a] -> integer -> a
kthSmallest(numbers, k)
Arguments
numbers
- An array of numbersk
- 0-indexed target (pass negative numbers for largest)