pub fn kth_smallest_heap<T>(input: &[T], k: usize) -> Option<T>
where
    T: Default + Ord + Copy,
{
    if input.len() < k {
        return None;
    }

    // heap will maintain the kth smallest elements
    // seen so far, when new elements, E_new arrives,
    // it is compared with the largest element of the
    // current Heap E_large, which is the current kth
    // smallest elements.
    // if E_new > E_large, then E_new cannot be the kth
    // smallest because there are already k elements smaller
    // than it
    // otherwise, E_large cannot be the kth smallest, and should
    // be removed from the heap and E_new should be added
    let mut heap = MaxHeap::new();

    // first k elements goes to the heap as the baseline
    for &val in input.iter().take(k) {
        heap.add(val);
    }

    for &val in input.iter().skip(k) {
        // compare new value to the current kth smallest value
        let cur_big = heap.next().unwrap(); // heap.next() can't be None
        match val.cmp(&cur_big) {
            Ordering::Greater => {
                heap.add(cur_big);
            }
            _ => {
                heap.add(val);
            }
        }
    }

    heap.next()
}
/// Returns k-th smallest element of an array.
/// Time complexity is stably O(nlog(k)) in all cases
/// Extra space is required to maintain the heap, and it doesn't
/// mutate the input list.
///
/// It is preferrable to the partition-based algorithm in cases when
/// we want to maintain the kth smallest element dynamically against
/// a stream of elements. In that case, once the heap is built, further
/// operation's complexity is O(log(k)).
Returns k-th smallest element of an array.
Time complexity is stably O(nlog(k)) in all cases
Extra space is required to maintain the heap, and it doesn't
mutate the input list.

It is preferrable to the partition-based algorithm in cases when
we want to maintain the kth smallest element dynamically against
a stream of elements. In that case, once the heap is built, further
operation's complexity is O(log(k)).
Returns k - th smallest element of an array . Time complexity is stably O ( nlog ( k )) in all cases Extra space is required to maintain the heap and it doesn ' t mutate the input list . It is preferrable to the partition - based algorithm in cases when we want to maintain the kth smallest element dynamically against a stream of elements . In that case once the heap is built further operation ' s complexity is O ( log ( k )) .
pub fn kth_smallest_heap<T>(input: &[T], k: usize) -> Option<T>
where
    T: Default + Ord + Copy,
{
    if input.len() < k {
        return None;
    }
    // heap will maintain the kth smallest elements
    // seen so far, when new elements, E_new arrives,
    // it is compared with the largest element of the
    // current Heap E_large, which is the current kth
    // smallest elements.
    // if E_new > E_large, then E_new cannot be the kth
    // smallest because there are already k elements smaller
    // than it
    // otherwise, E_large cannot be the kth smallest, and should
    // be removed from the heap and E_new should be added
    let mut heap = MaxHeap::new();
    // first k elements goes to the heap as the baseline
    for &val in input.iter().take(k) {
        heap.add(val);
    }
    for &val in input.iter().skip(k) {
        // compare new value to the current kth smallest value
        let cur_big = heap.next().unwrap(); // heap.next() can't be None
        match val.cmp(&cur_big) {
            Ordering::Greater => {
                heap.add(cur_big);
            }
            _ => {
                heap.add(val);
            }
        }
    }
    heap.next()
}
pub fn kth_smallest_heap < T > ( input : & [ T ] , k : usize ) -> Option < T > where T : Default + Ord + Copy , { if input . len ( ) < k { return None ; } // heap will maintain the kth smallest elements // seen so far, when new elements, E_new arrives, // it is compared with the largest element of the // current Heap E_large, which is the current kth // smallest elements. // if E_new > E_large, then E_new cannot be the kth // smallest because there are already k elements smaller // than it // otherwise, E_large cannot be the kth smallest, and should // be removed from the heap and E_new should be added let mut heap = MaxHeap :: new ( ) ; // first k elements goes to the heap as the baseline for & val in input . iter ( ) . take ( k ) { heap . add ( val ) ; } for & val in input . iter ( ) . skip ( k ) { // compare new value to the current kth smallest value let cur_big = heap . next ( ) . unwrap ( ) ; // heap.next() can't be None match val . cmp ( & cur_big ) { Ordering :: Greater => { heap . add ( cur_big ) ; } _ => { heap . add ( val ) ; } } } heap . next ( ) }
Returns k-th smallest element of an array.
Returns k - th smallest element of an array .
input
&[T]
k
usize
input
&[T]
k
usize