Skip to content

Instantly share code, notes, and snippets.

@xiabingquan
Created February 11, 2025 15:14
Show Gist options
  • Save xiabingquan/b3091b4f390b2c950252265ae93b4cbf to your computer and use it in GitHub Desktop.
Save xiabingquan/b3091b4f390b2c950252265ae93b4cbf to your computer and use it in GitHub Desktop.
A minimal example of insert sort in Rust
fn insert_sort<T: Ord + Clone>(arr: &mut [T]) {
for i in 0..arr.len() - 1 {
let val = arr[i + 1].clone();
let mut j = i as isize;
while j >= 0 && val > arr[j as usize] {
j -= 1;
}
for k in ((j+1) as usize..i+1).rev() {
arr[k + 1] = arr[k].clone();
}
arr[(j + 1) as usize] = val;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment