奇偶排序
pub fn odd_even_sort<T: Ord>(arr: &mut [T]) {
let len = arr.len();
if len == 0 {
return;
}
let mut sorted = false;
while !sorted {
sorted = true;
for i in (1..len - 1).step_by(2) {
if arr[i] > arr[i + 1] {
arr.swap(i, i + 1);
sorted = false;
}
}
for i in (0..len - 1).step_by(2) {
if arr[i] > arr[i + 1] {
arr.swap(i, i + 1);
sorted = false;
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn basic() {
let mut arr = vec![3, 5, 1, 2, 4, 6];
odd_even_sort(&mut arr);
assert_eq!(arr, vec![1, 2, 3, 4, 5, 6]);
}
#[test]
fn empty() {
let mut arr = Vec::<i32>::new();
odd_even_sort(&mut arr);
assert_eq!(arr, vec![]);
}
#[test]
fn one_element() {
let mut arr = vec![3];
odd_even_sort(&mut arr);
assert_eq!(arr, vec![3]);
}
#[test]
fn pre_sorted() {
let mut arr = vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
odd_even_sort(&mut arr);
assert_eq!(arr, vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
}
}
当前内容版权归 rustlang-cn 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 rustlang-cn .