select

Selects the kth element of an array as defined by the given predicate.

Notes: -> Uses the Quickselect selection algorithm (http://en.wikipedia.org/wiki/Quickselect) -> Typically, Quickselect is used to select the kth smallest element of an array, but this function can also be used to select elements ordered in any fashion, as defined by the given predicate. -> The array elements may be reordered during the selection process. -> The following would be true if the selection happens successfully (i.e. if no exception is thrown): * the kth element in the array as defined by the given predicate would have been moved to index 'k'. * All elements before index 'k' are guaranteed to be passed by the predicate compared to element 'k' (i.e. the predicate would return true) whereas all elements after index 'k' are guaranteed to not be passed by the predicate compared to element 'k' (i.e. the predicate would return false). This means that, if the kth smallest element is being selected, then all elements before the kth smallest element would be lesser than it and all elements after the kth smallest element would be greater than or equal to it. However, no guarantees are made about the sorting order of the elements before/after the kth smallest element. In other words, unordered partial sorting of the array will take place. -> The result is defined only if all values in the array are ordered (unordered values like floating-point NaNs could result in undefined behaviour).

size_t
select
()
(
T[] arr
,
size_t k
,
Pred pred = Pred.init
)

Parameters

arr T[]

the array being worked upon.

k size_t

the order statistic being looked for (starting from zero). In particular, there should be 'k' elements in the array for which the predicate will return true.

pred Pred

predicate used for array element comparisons. Takes two array elements to be compared and returns true/false based upon the comparison. (defaults to a comparison using "<" if no predicate is given)

Return Value

Type: size_t

the index of the kth element in the array as defined by the ordering predicate.

Throws

new Exception if input array is empty.

Meta