Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
fn insertion_sort<T: PartialOrd>(xs: &mut Vec<T>) {
for i in 1..(xs.len() - 1) {
let mut j = i;
while j > 0 && xs[j-1] > xs[j] {
xs.swap(j, j-1);
j -= 1;
}
}
}
fn main() {
let mut v: Vec<i32> = vec![1, 3, 2, 3, 4];
insertion_sort(&mut v);
for e in v {
println!("{}", e);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.