algosort/sorts/
insertion_sort.rs

1use super::SortAlgo;
2
3pub struct InsertionSort;
4
5impl<T: Ord + Copy> SortAlgo<T> for InsertionSort {
6    fn sort(&self, array: &mut [T]) {
7        for i in 1..array.len() {
8            let key = array[i];
9            let mut j = (i - 1) as i64;
10
11            while j >= 0 && array[j as usize] > key {
12                array[(j + 1) as usize] = array[j as usize];
13                j -= 1;
14            }
15
16            array[(j + 1) as usize] = key;
17        }
18    }
19}