fn main() {
println!("{:#?}", merge_sort(&[5,4,1,6,8,2,3,7]));
}
fn merge_sort(array: &[i32]) -> Vec<i32>{
if array.len() == 1{
return array.to_owned();
}
let mid = array.len() / 2;
let one_half = &array[..mid];
let second_half = &array[mid..];
let slice_1 = merge_sort(one_half);
let slice_2 = merge_sort(second_half);
merge(&slice_1, &slice_2)
}
fn merge(first_array: &[i32], second_array: &[i32]) -> Vec<i32>{
let mut temp = Vec::new();
let mut ptr_1 = 0;
let mut ptr_2 = 0;
while ptr_1 < first_array.len() && ptr_2 < second_array.len(){
let element_1 = first_array[ptr_1];
let element_2 = second_array[ptr_2];
if element_1 < element_2{
temp.push(element_1);
ptr_1 += 1;
}else{
temp.push(element_2);
ptr_2 += 1;
}
}
if ptr_1 < first_array.len(){
temp.extend(&first_array[ptr_1..]);
}
if ptr_2 < second_array.len(){
temp.extend(&second_array[ptr_2..]);
}
temp
}