#median #partition #select #no-std #nth-element

no-std kth

Find the k-th order element of an array (one of which is the median)

1 unstable release

Uses old Rust 2015

0.1.0 May 29, 2017

#20 in #median

Download history 22/week @ 2024-07-22 37/week @ 2024-07-29 17/week @ 2024-08-05 12/week @ 2024-08-12 24/week @ 2024-08-19 68/week @ 2024-08-26 47/week @ 2024-09-02 37/week @ 2024-09-09 69/week @ 2024-09-16 112/week @ 2024-09-23 101/week @ 2024-09-30 95/week @ 2024-10-07 54/week @ 2024-10-14 104/week @ 2024-10-21 108/week @ 2024-10-28 62/week @ 2024-11-04

332 downloads per month
Used in 3 crates

Apache-2.0 OR MIT

18KB
340 lines

kth

Methods for partitioning arrays & finding the k-th order element.

The median is the element with order equal to half of the array length.

crates.io

travis-ci

docs (release)

docs (master)

Rust RFC issue #1470 talks about adding a similar interface.


lib.rs:

Algorithms to find the K-th order element

The k-th order element is the element which would be at the k-th index if the array was sorted.

Finding the median is a special case of finding the k-th order element, to find the median, select k to be half the array length.

Partitioning is commonly performed when searching for the K-th order element. An array is partitioned if all elements before a given element X are less than X, and all elements after a that same element X are greater than X.

Example

use kth::SliceExtKth;

let mut x = [6, 6, 8 ,1, 2];
// sorted =  1  2  6  6  8
let m = x.len()/2;
x.partition_by_kth(m);
println!("Median is {}", x[m]);
assert_eq!(x[x.len()/2], 6);

Dependencies

~25KB