Skip to content

Instantly share code, notes, and snippets.

@eduardonunesp
Created January 24, 2023 16:17
Show Gist options
  • Save eduardonunesp/058a64ae614390776f4ba3afddeb819f to your computer and use it in GitHub Desktop.
Save eduardonunesp/058a64ae614390776f4ba3afddeb819f to your computer and use it in GitHub Desktop.
fn main() {
println!("Sort numbers ascending");
let mut numbers = [4, 65, 2, -31, 0, 99, 2, 83, 782, 1];
println!("Before: {:?}", numbers);
bubble_sort(&mut numbers);
println!("After: {:?}\n", numbers);
println!("Sort strings alphabetically");
let mut strings = ["beach", "hotel", "airplane", "car", "house", "art"];
println!("Before: {:?}", strings);
bubble_sort(&mut strings);
println!("After: {:?}\n", strings);
}
pub fn bubble_sort<T: Ord>(arr: &mut [T]) {
for i in 0..arr.len() {
for j in 0..arr.len() - 1 - i {
if arr[j] > arr[j + 1] {
arr.swap(j, j + 1);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment