Created
February 11, 2025 15:14
-
-
Save xiabingquan/b3091b4f390b2c950252265ae93b4cbf to your computer and use it in GitHub Desktop.
A minimal example of insert sort in Rust
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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