#sorting #sort #float

yanked quickersort

Fast sorting compatible with stable Rust. Also has (optional) support for efficient and robust sorting of floating point numbers

Uses old Rust 2015

3.0.1 Jan 30, 2018
3.0.0 May 30, 2017
2.2.0 Dec 20, 2016
2.1.1 Oct 14, 2016
1.0.0 Jul 30, 2015

#58 in #sorting

Download history 140/week @ 2023-02-06 195/week @ 2023-02-13 236/week @ 2023-02-20 95/week @ 2023-02-27 258/week @ 2023-03-06 189/week @ 2023-03-13 121/week @ 2023-03-20 160/week @ 2023-03-27 160/week @ 2023-04-03 128/week @ 2023-04-10 120/week @ 2023-04-17 190/week @ 2023-04-24 247/week @ 2023-05-01 131/week @ 2023-05-08 156/week @ 2023-05-15 106/week @ 2023-05-22

725 downloads per month
Used in 9 crates (4 directly)


509 lines


There's really no point in using this library any more. Everything good about it has been incorporated into std::sort_unstable in the Rust standard library, which even uses a better algorithm. Just use that.


Build Status Crates.IO


This is an implementation of the introsort sorting algorithm.

This is a fork of veddan/rust-introsort, with a number of improvements.

To use with cargo, add the following to your Cargo.toml:

quickersort = "3.0"

and in your crate root, add

extern crate quickersort;


The interface is similar to the standard library sort and sort_by functions.

An example:

extern crate quickersort;

fn main() {
    let mut ss = vec!["Introsort", "or", "introspective", "sort", "is",
                      "a", "hybrid", "sorting", "algorithm", "that",
                      "provides", "both", "fast", "average",
                      "performance", "and", "(asymptotically)", "optimal",
                      "worst-case", "performance"];
    quickersort::sort(&mut ss[..]);
    for s in ss.iter() { println!("\t{}", s); }
    quickersort::sort_by(&mut ss[..], &|a, b| a.len().cmp(&b.len()));
    println!("\nby length");
    for s in ss.iter() { println!("\t{}", s); }

Unlike the standard library sort function, introsort is not a stable sort.


At its heart, it is a dual-pivot quicksort. For partition with many equal elements, it will instead use a single-pivot quicksort optimized for this case. It detects excessive recursion during quicksort and switches to heapsort if need be, guaranteeing O(n log(n)) runtime on all inputs. For small partitions it uses insertion sort instead of quicksort.

Unlike the std sort, it does not allocate.


It is quite fast, outperforming the standard sort on all data sets I have tried. The performance difference varies depending on the characteristics of the data. On large, completely random arrays, introsort is only 5-10% faster than the standard sort. However, introsort's performance is greatly improved if the data has few unique values or is (partially) sorted (including reversed data). For sorted data, introsort is ~4-5 times faster, and for data with few unique values it can be more than 20 times faster.

Detailed benchmark data (only for integers as of now) is available.

Floating point

The crate, if built with the "float" feature (which is the default), also includes a sort_floats function. Floating point numbers are not Ord, only PartialOrd, so sort can not be used on them. The ordering used by sort_floats is

| -inf | < 0 | -0 | +0 | > 0 | +inf | NaN |

sort_floats is much more efficient than passing a comparator function implementing this ordering to sort_by.