Skip to content

Instantly share code, notes, and snippets.

@StephanieSunshine
Created December 7, 2021 13:28
Show Gist options
  • Save StephanieSunshine/05617e25fa28c58c2ef1bc1484692d2e to your computer and use it in GitHub Desktop.
Save StephanieSunshine/05617e25fa28c58c2ef1bc1484692d2e to your computer and use it in GitHub Desktop.
Insertsort in Rust
// input: uint32 list
// output: uint32 list
// implement an insert sort over a list of uint32 in Rust
pub fn sort(l: &mut Vec<u32>) -> &mut Vec<u32> {
// iterate through the list starting on the second one
for i in 1..(*l).len() {
let mut t = i; // we are going to walk with this
let mut try_again = true; // just to init the while loop
while try_again {
try_again = false; // lets not unless something happens
if (*l)[t-1] > (*l)[t] {
// swap dec t by 1 and if t is greater than 0 try again
let s = (*l)[t];
(*l)[t] = (*l)[t-1];
(*l)[t-1] = s;
if t > 1 {
t -= 1;
try_again = true;
}
}
}
}
l
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment