A Floyd-Rivest quick selection algorithm.
Rearranges the array items so that all items in the [left, k] range are smaller than all items in [k, right]; The k-th element will have the (k - left + 1)th smallest value in [left, right].
The array is modified in-place.
Based on code by Vladimir Agafonkin
name | type | arguments | description |
---|---|---|---|
arr | array |
The array to sort. |
|
k | number |
The k-th element index. |
|
left | number | <optional> |
The index of the left part of the range. |
right | number | <optional> |
The index of the right part of the range. |
compare | function | <optional> |
An optional comparison function. Is passed two elements and should return 0, 1 or -1. |