Rust Quick Sort Example

Published 361d ago

 nptester

A simple example of quick sort in Rust, including a main function.

fn quick_sort(arr: &mut [i32]) {
    if arr.len() <= 1 {
        return;
    }
    let pivot_index = partition(arr);
    quick_sort(&mut arr[0..pivot_index]);
    quick_sort(&mut arr[pivot_index + 1..]);
}

fn partition(arr: &mut [i32]) -> usize {
    let pivot = arr[arr.len() - 1];
    let mut i = 0;
    for j in 0..arr.len() - 1 {
        if arr[j] < pivot {
            arr.swap(i, j);
            i += 1;
        }
    }
    arr.swap(i, arr.len() - 1);
    i
}

fn main() {
    let mut numbers = [3, 7, 5, 2, 9, 1, 6];
    quick_sort(&mut numbers);
    println!("{:?}", numbers);
}

sortingalgorithmquick sortRust
1

Please login or sign up to comment and collaborate