(a: &mut [T])
| 5 | use std::cmp::Ordering; |
| 6 | |
| 7 | pub fn sort<T>(a: &mut [T]) |
| 8 | where |
| 9 | T: Ord, |
| 10 | { |
| 11 | let len = a.len(); |
| 12 | // i begins with `1` |
| 13 | for i in 1..len { |
| 14 | // insert a[i] into a[0..i-1] |
| 15 | let mut j = i; |
| 16 | while j > 0 && a[j] < a[j - 1] { |
| 17 | a.swap(j, j - 1); |
| 18 | j -= 1; |
| 19 | } |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | /// insertion sort a[lo..=hi], starting at d-th character |
| 24 | /// lo & hi, is inclusive |