pub fn shell_sort<T: Ord + Copy>(values: &mut Vec<T>) {
fn insertion_with_gap<T: Ord + Copy>(arr: &mut [T], start: usize, gap: usize) {
for index in ((start + gap)..arr.len()).step_by(gap) {
let current_value = arr[index];
let mut current_index = index;
while current_index >= gap && arr[current_index - gap] > current_value {
arr[current_index] = arr[current_index - gap];
current_index -= gap;
}
arr[current_index] = current_value;
}
}
let mut gap = values.len() / 2;
while gap > 0 {
for start in 0..gap {
insertion_with_gap(values, start, gap);
}
gap /= 2;
}
}
本作品採用《CC 協議》,轉載必須註明作者和本文連結