2个版本
0.1.1 | 2024年3月20日 |
---|---|
0.1.0 | 2024年3月19日 |
#676 在 算法 中
每月42次下载
10KB
137 行
qsort-rs
一种接受任何类型和非递归方法的快速排序算法。
如何使用此函数
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()
}
]
)
}