Created
January 24, 2023 16:17
-
-
Save eduardonunesp/058a64ae614390776f4ba3afddeb819f to your computer and use it in GitHub Desktop.
This file contains 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 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