bubble sort in rust

main.rs
fn bubble_sort(arr: &mut [i32]) {
    let n = arr.len();
    for i in 0..n {
        for j in 0..n-i-1 {
            if arr[j] > arr[j+1] {
                arr.swap(j, j+1);
            }
        }
    }
}

fn main() {
    let mut arr = [64, 25, 12, 22, 11];
    println!("Array before sorting: {:?}", arr);
    
    bubble_sort(&mut arr);
    
    println!("Array after sorting: {:?}", arr);
}
395 chars
20 lines

related categories

gistlibby LogSnag