Answers for "quicksort rust"

0

quicksort rust

fn swap(param_int_array: &mut [i32], i: usize, j: usize ) {
    let temp = param_int_array[i];
    param_int_array[i] = param_int_array[j];
    param_int_array[j] = temp;
}

fn partition(param_int_array: &mut [i32], start: usize, end: usize ) -> i32 {
    let mut pivot = param_int_array[end];
    let mut index = start;
    let mut i = start;
    while i < end {
        if param_int_array[i] < pivot {
            swap(param_int_array, i, index);
            index += 1;
        }
        i += 1;
    }
    swap(param_int_array, index, end);
    return index as i32;
}

fn quick_sort(param_int_array: &mut [i32], start: usize, end: usize) {
    if start >= end {
        return;
    }
    let pivot = partition(param_int_array, start, end);
    println!("=={}", pivot);
    quick_sort(param_int_array, start, (pivot - 1) as usize);
    quick_sort(param_int_array, (pivot + 1) as usize, end);
}

fn main() {
    let mut int_values_array:[i32; 13] = [1, 0, 9, 8, 100, 345, 5, 6,7 , 4, 3, 2, 1];
    let last_index = int_values_array.len() - 1;
    quick_sort(&mut int_values_array, 0, last_index);
    println!("{:?}", int_values_array)
}
Posted by: Guest on September-19-2021

Browse Popular Code Answers by Language