#sorting #permutation

co_sort

Sort arrays given a permutation

4 releases

0.2.0 Feb 28, 2019
0.1.2 Feb 28, 2019
0.1.1 Feb 28, 2019
0.1.0 Feb 28, 2019

#1942 in Algorithms

Download history 733/week @ 2024-03-13 864/week @ 2024-03-20 552/week @ 2024-03-27 514/week @ 2024-04-03 661/week @ 2024-04-10 297/week @ 2024-04-17 166/week @ 2024-04-24 211/week @ 2024-05-01 193/week @ 2024-05-08 201/week @ 2024-05-15 403/week @ 2024-05-22 338/week @ 2024-05-29 209/week @ 2024-06-05 179/week @ 2024-06-12 161/week @ 2024-06-19 184/week @ 2024-06-26

862 downloads per month
Used in 9 crates (2 directly)

MIT/Apache

14KB
206 lines

co_sort

Sort multiple arrays given a permutation.

LICENSE LICENSE Crates.io Documentation

The simplest way to use this crate is the co_sort! macro, it will sort the first array and swap elements of the others in order to mimic the changes in the first array.
Usefull when you have multiple slices with an implicit relation.

#[macro_use] extern crate co_sort;
use co_sort::*;
let mut names = ["Diego", "Maia", "Luciana", "Bruno", "Astrid", "Thierry"];
let mut ages =  [  73,      88,      21,        47,      4,        62    ];
// We want to sort the names but keep the ages synced
co_sort![names, ages];
assert_eq!(names, ["Astrid", "Bruno", "Diego", "Luciana", "Maia", "Thierry"]);
assert_eq!(ages,  [   4,       47,      73,       21,       88,      62    ]);

If you want more control you can use the functions co_sort and co_sort_stable, the macro uses co_sort internally.
co_sort_stable allocates O(n) memory and requires the types to implement Clone while co_sort is in place and doesn't require any trait.
Performance wise co_sort scale well with the number of arrays but not with their size and co_sort_stable is the opposite.

# use co_sort::*;
let mut names = ["Diego", "Maia", "Luciana", "Bruno", "Astrid", "Thierry"];
let mut ages =  [  73,      88,      21,       47,       4,        62    ];
let permutation = Permutation::from(names.as_ref());
permutation.co_sort((names.as_mut(), ages.as_mut()));
// or permutation.co_sort_stable((names.as_mut(), ages.as_mut()));
assert_eq!(names, ["Astrid", "Bruno", "Diego", "Luciana", "Maia", "Thierry"]);
assert_eq!(ages,  [   4,       47,      73,       21,       88,      62    ]);

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

No runtime deps