#sorting #adaptive

dmsort

Fast adaptive sorting for when most of your data is already in order. dmsort can be 2-5 times faster than Rust's default sort when more than 80% of the elements are already in order

7 releases (3 stable)

Uses old Rust 2015

1.0.2 Jun 19, 2022
1.0.1 Oct 25, 2020
1.0.0 Jul 18, 2018
0.1.3 May 8, 2017
0.1.1 Jan 6, 2017

#263 in Algorithms

Download history 3939/week @ 2024-08-19 4121/week @ 2024-08-26 5087/week @ 2024-09-02 3823/week @ 2024-09-09 3284/week @ 2024-09-16 5359/week @ 2024-09-23 5439/week @ 2024-09-30 5195/week @ 2024-10-07 7925/week @ 2024-10-14 4358/week @ 2024-10-21 2293/week @ 2024-10-28 1677/week @ 2024-11-04 2234/week @ 2024-11-11 1889/week @ 2024-11-18 1731/week @ 2024-11-25 2290/week @ 2024-12-02

8,277 downloads per month
Used in 21 crates (4 directly)

MIT license

20KB
263 lines

Drop-Merge sort created and implemented by Emil Ernerfeldt.

Drop-Merge sort is an adaptive, unstable sorting algorithm designed for nearly-sorted data. An example use-case would be re-sorting an already sorted list after minor modifications.

Drop-Merge sort is especially useful for:

  • Long lists (>10k elements)
  • Where >80% of the data is already in-order
  • The unsorted elements are evenly distributed.

Expected number of comparisons is O(N + K * log(K)) where K is the number of elements not in order. Expected memory usage is O(K). Works best when K < 0.2 * N. The out-of-order elements are expected to be randomly distributed (NOT clumped).

Examples

extern crate dmsort;

fn main() {
    let mut numbers : Vec<i32> = vec!(0, 1, 6, 7, 2, 3, 4, 5);

    // Sort with custom key:
    dmsort::sort_by_key(&mut numbers, |x| -x);
    assert_eq!(numbers, vec!(7, 6, 5, 4, 3, 2, 1, 0));

    // Sort with Ord trait:
    dmsort::sort(&mut numbers);
    assert_eq!(numbers, vec!(0, 1, 2, 3, 4, 5, 6, 7));

    // Sort with custom compare:
    dmsort::sort_by(&mut numbers, |a, b| b.cmp(a));
    assert_eq!(numbers, vec!(7, 6, 5, 4, 3, 2, 1, 0));
}

No runtime deps