Skip to content

Instantly share code, notes, and snippets.

@kevinquinnyo
Created January 6, 2018 20:24
Show Gist options
  • Save kevinquinnyo/d9beead2baa17a4da1e22c59c0f4dde6 to your computer and use it in GitHub Desktop.
Save kevinquinnyo/d9beead2baa17a4da1e22c59c0f4dde6 to your computer and use it in GitHub Desktop.
bubble sort in rust attempt 1
fn main() {
let mut array: [i32; 5] = [5, 6, 3, 2, 8];
let sorted = bubble_sort(array);
for x in &sorted {
println!("{0}", x);
}
assert_eq!(sorted, [2, 3, 5, 6, 8], "Failed to sort array.");
}
fn bubble_sort(mut array: [i32; 5]) -> [i32; 5] {
for i in 0..array.len() {
for j in 0..array.len() - i - 1 {
if array[j] > array[j + 1] {
let tmp = array[j];
array[j] = array[j + 1];
array[j + 1] = tmp;
}
}
}
return array;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment