Pattern-defeating quicksort
This sort is significantly faster than the standard sort in Rust. In particular, it sorts random arrays of integers approximately 45% faster. The key drawback is that it is an unstable sort (i.e. may reorder equal elements). However, in most cases stability doesn't matter anyway.
The algorithm is based on pdqsort by Orson Peters, published at: https://github.com/orlp/pdqsort
Now in nightly Rust
If you're using nightly Rust, you don't need this crate. The sort is as of recently implemented in libcore.
Use it like this:
let mut v = ;
v.sort_unstable;
assert!;
See The Unstable Book for more information.
Properties
- Best-case running time is
O(n)
. - Worst-case running time is
O(n log n)
. - Unstable, i.e. may reorder equal elements.
- Does not allocate additional memory.
- Uses
#![no_std]
.
Examples
extern crate pdqsort;
let mut v = ;
sort;
assert!;
sort_by;
assert!;
sort_by_key;
assert!;
A simple benchmark
Sorting 10 million random numbers of type u64
:
Sort | Time |
---|---|
pdqsort | 370 ms |
slice::sort | 668 ms |
quickersort | 777 ms |
rdxsort | 602 ms |