Crates.io | csselection_sort |
lib.rs | csselection_sort |
version | 0.1.1 |
source | src |
created_at | 2024-02-18 02:05:52.913879 |
updated_at | 2024-02-18 02:09:22.873193 |
description | Selection sort implementation for indexable collections |
homepage | |
repository | https://github.com/cstffx/csselection_sort |
max_upload_size | |
id | 1143698 |
size | 5,717 |
Selection sort algorithm implementation over a vector.
Use this create to add a selection_sort
and selection_sort_by
methods
to Vec data type.
use csselection_sort::SelectionSort;
let mut input: Vec<u32> = vec![2, 1];
// this will sort items in ascending order
// using selection sort algorithm
input.selection_sort();
// input is now [1,2]
Use selection_sort_by
to customize how items are compared.
As second argument pass a function with the form:
fn(a: &T, b: &T) -> bool;
This function must return true
to indicate that a
is further to the left than b
.
For example, to sort a list of u32
, in descending order, by
parameter can be:
fn desc(a: u32, b: u32) -> bool {
a < b
}
This example returns true
when a=1
and b==5
in order to
ensure that 5
is located at the left o 1
.
For convenience asc
and desc
functions are provided.
use csselection_sort::{SelectionSort, des};
let mut input: Vec<u32> = vec![1, 2];
// this will sort items in descending order
// using selection sort algorithm
input.selection_sort_by(desc);
// input is now [2,1]
To call selection_sort
is equivalent to call selection_sort_by(asc)
.