梳排序
pub fn comb_sort<T: Ord>(arr: &mut [T]) {
let mut gap = arr.len();
let shrink = 1.3;
let mut sorted = false;
while !sorted {
gap = (gap as f32 / shrink).floor() as usize;
if gap <= 1 {
gap = 1;
sorted = true;
}
for i in 0..arr.len() - gap {
let j = i + gap;
if arr[i] > arr[j] {
arr.swap(i, j);
sorted = false;
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn descending() {
//descending
let mut ve1 = vec![6, 5, 4, 3, 2, 1];
comb_sort(&mut ve1);
for i in 0..ve1.len() - 1 {
assert!(ve1[i] <= ve1[i + 1]);
}
}
#[test]
fn ascending() {
//pre-sorted
let mut ve2 = vec![1, 2, 3, 4, 5, 6];
comb_sort(&mut ve2);
for i in 0..ve2.len() - 1 {
assert!(ve2[i] <= ve2[i + 1]);
}
}
}
当前内容版权归 rustlang-cn 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 rustlang-cn .