MCPcopy Create free account
hub / github.com/TheAlgorithms/Go / FindKthMin

Function FindKthMin

math/kthnumber.go:19–22  ·  view source on GitHub ↗

FindKthMin returns kth small element given an integer slice with nil `error` if found and returns -1 with `error` `search.ErrNotFound` if not found. NOTE: The `nums` slice gets mutated in the process.

(nums []int, k int)

Source from the content-addressed store, hash-verified

17// with nil `error` if found and returns -1 with `error` `search.ErrNotFound`
18// if not found. NOTE: The `nums` slice gets mutated in the process.
19func FindKthMin(nums []int, k int) (int, error) {
20 index := k - 1
21 return kthNumber(nums, index)
22}
23
24// kthNumber use the selection algorithm (based on the partition method - the same one as used in quicksort).
25func kthNumber(nums []int, k int) (int, error) {

Callers 1

TestFindKthMinFunction · 0.85

Calls 1

kthNumberFunction · 0.85

Tested by 1

TestFindKthMinFunction · 0.68