pub fn odd_even_sort<T: Ord>(arr: &mut [T]) {
let len = arr.len();
if len < 2 {
return ;
}
let mut sorted = false;
while !sorted {
sorted = true;
// odd
for current in (1..len-1).step_by(2) {
if arr[current] > arr[current + 1] {
arr.swap(current, current + 1);
sorted = false;
}
}
// even
for current in (0..len-1).step_by(2) {
if arr[current] > arr[current + 1] {
arr.swap(current, current + 1);
sorted = false;
}
}
}
}
rust-algorithms:10-奇偶排序
Rust
360
0
0
2022-11-10
登录后可点赞和收藏
登录后可点赞和收藏