Skip to content

Instantly share code, notes, and snippets.

@gnuvince
Created October 13, 2015 02:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gnuvince/152cf2e304935b2faa08 to your computer and use it in GitHub Desktop.
Save gnuvince/152cf2e304935b2faa08 to your computer and use it in GitHub Desktop.
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