select

fun <T> select(arr: Array<T>, from: Int, to: Int, k: Int, comparator: Comparator<in T>)

Reorganize arr[from:to[ so that the element at offset k is at the same position as if arr[from:to] was sorted, and all elements on its left are less than or equal to it, and all elements on its right are greater than or equal to it.

This runs in linear time on average and in n log(n) time in the worst case.

Parameters

arr

Array to be re-organized.

from

Starting index for re-organization. Elements before this index will be left as is.

to

Ending index. Elements after this index will be left as is.

k

Index of element to sort from. Value must be less than 'to' and greater than or equal to 'from'.

comparator

Comparator to use for sorting