pub fn shell_sort(arr: &mut [i32]) {
let len = arr.len() as i32;
let mut h: i32 = 1;
while h <= len / 3 {
h = h * 3 + 1;
}
while h > 0 {
for i in h..len {
let mut j = i;
let temp = arr[i as usize];
while j >= h && arr[(j - h) as usize] > temp {
arr[j as usize] = arr[(j - h) as usize];
j -= h;
}
arr[j as usize] = temp;
}
h = (h - 1) / 3;
}
}
fn main() {
let mut arr = [12, 34, 54, 2, 3];
shell_sort(&mut arr);
println!("{:?}", arr);
}
rust实现希尔排序算法
最新推荐文章于 2024-08-26 20:14:29 发布