Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find the kth smallest element in an unsorted array or its segment?

Tags:

Does Rust have a function to find the kth smallest element in an array or a segment of an array?

(Similar to std::nth_element in C++)

like image 345
MWB Avatar asked Sep 30 '16 00:09

MWB


People also ask

What is the data structure to be used to find kth smallest element?

There is a much better approach to finding Kth smallest element, which relies on median-of-medians algorithm. Basically any partition algorithm would be good enough on average, but median-of-medians comes with the proof of worst-case O(N) time for finding the median.

What is the lowest worst case time complexity of finding kth smallest element of an unsorted array?

Finding the kth smallest element in an array with sorting To execute this, We first sort the array then access its k-1th index, which contains the kth smallest element of the array. K'th smallest element is 45. The time complexity of this method is O(N*logN) because of the sorting algorithm used in it.


2 Answers

Yes (since rust 1.49)

select_nth_unstable

like image 85
georeth Avatar answered Sep 23 '22 13:09

georeth


I don't think there is such a function in std.

However, you can use the crate order_stat, which offers a kth function.

like image 36
Veedrac Avatar answered Sep 24 '22 13:09

Veedrac