Skip to content

Instantly share code, notes, and snippets.

@rylev rylev/main.rs
Created Sep 12, 2018

Embed
What would you like to do?
Bubble Sort In Rust
fn main() {
let mut sortable: [i32; 5] = [5, 8, 2, 7, 6];
let length = sortable.len();
let mut swapped = true;
print_array(&sortable);
while swapped {
swapped = false;
for i in 1..length {
let previous_element = sortable[i - 1];
let current_element = sortable[i];
if previous_element > current_element {
sortable.swap(i - 1, i);
swapped = true;
}
}
}
print_array(&sortable);
}
fn print_array<T: std::fmt::Display>(array: &[T]) {
print!("[");
for (i, elem) in array.iter().enumerate() {
print!("{}", elem);
if i != array.len() - 1 {
print!(",");
}
}
println!("]");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.