2 releases
0.1.1 | Mar 20, 2024 |
---|---|
0.1.0 | Mar 19, 2024 |
#1082 in Algorithms
42 downloads per month
10KB
137 lines
qsort-rs
A quick sort algorithm that accepts any type and non-recursive approach.
How to use this function
use qsort_rs::qsort;
#[derive(Ord, PartialEq, PartialOrd, Eq, Debug)]
struct Student {
name: String,
age: u32,
gender: String,
}
fn main() {
let mut arr = [
Student {
name: String::from("Zed"),
age: 3,
gender: String::from("Male"),
},
Student {
name: String::from("Aubrey"),
age: 13,
gender: String::from("Female"),
},
Student {
name: String::from("Jaime"),
age: 6,
gender: String::from("Male"),
},
Student {
name: String::from("Irene"),
age: 8,
gender: String::from("Female"),
},
Student {
name: String::from("Ren"),
age: 9,
gender: String::from("Male"),
},
];
qsort::sort(&mut arr, |low, high| low.name <= high.name);
assert_eq!(
arr,
[
Student {
name: "Aubrey".to_string(),
age: 13,
gender: "Female".to_string()
},
Student {
name: "Irene".to_string(),
age: 8,
gender: "Female".to_string()
},
Student {
name: "Jaime".to_string(),
age: 6,
gender: "Male".to_string()
},
Student {
name: "Ren".to_string(),
age: 9,
gender: "Male".to_string()
},
Student {
name: "Zed".to_string(),
age: 3,
gender: "Male".to_string()
}
]
)
}