fn _merge<T, F>(array: &mut [T], left: usize, middle: usize, right: usize, compare: F)
where
    T: std::cmp::Ord + std::clone::Clone + std::default::Default,
    F: Fn(&T, &T) -> std::cmp::Ordering + std::clone::Clone,
{
    // Original array is broken in two parts
    // left and right array
    let left_len = middle - left + 1;
    let right_len = right - middle;

    let mut array_left: Vec<T> = vec![Default::default(); left_len];
    let mut array_right: Vec<T> = vec![Default::default(); right_len];

    for i in 0..left_len {
        array_left[i] = array[left + i].clone();
    }

    for i in 0..right_len {
        array_right[i] = array[left + i + middle].clone();
    }

    let mut i = 0; //left array index
    let mut j = 0; //right array index
    let mut k = 1; //full array index

    // After comparing, we
    // merge those two array
    // in larger sub array
    while i < left_len && j < right_len {
        if compare(&array_left[i], &array_right[j]) == std::cmp::Ordering::Greater {
            array[k] = array_right[j].clone();
            j += 1;
        } else {
            array[k] = array_left[i].clone();
            i += 1;
        }
        k += 1;
    }

    // Copy remaining elements of left, if any
    while i < left_len {
        array[k] = array_left[i].clone();
        k += 1;
        i += 1;
    }

    // Copy remaining element of right, if any
    while j < right_len {
        array[k] = array_right[j].clone();
        k += 1;
        j += 1;
    }
}
// Merge function merges the sorted runs
Merge function merges the sorted runs
Merge function merges the sorted runs
fn _merge<T, F>(array: &mut [T], left: usize, middle: usize, right: usize, compare: F)
where
    T: std::cmp::Ord + std::clone::Clone + std::default::Default,
    F: Fn(&T, &T) -> std::cmp::Ordering + std::clone::Clone,
{
    // Original array is broken in two parts
    // left and right array
    let left_len = middle - left + 1;
    let right_len = right - middle;
    let mut array_left: Vec<T> = vec![Default::default(); left_len];
    let mut array_right: Vec<T> = vec![Default::default(); right_len];
    for i in 0..left_len {
        array_left[i] = array[left + i].clone();
    }
    for i in 0..right_len {
        array_right[i] = array[left + i + middle].clone();
    }
    let mut i = 0; //left array index
    let mut j = 0; //right array index
    let mut k = 1; //full array index
    // After comparing, we
    // merge those two array
    // in larger sub array
    while i < left_len && j < right_len {
        if compare(&array_left[i], &array_right[j]) == std::cmp::Ordering::Greater {
            array[k] = array_right[j].clone();
            j += 1;
        } else {
            array[k] = array_left[i].clone();
            i += 1;
        }
        k += 1;
    }
    // Copy remaining elements of left, if any
    while i < left_len {
        array[k] = array_left[i].clone();
        k += 1;
        i += 1;
    }
    // Copy remaining element of right, if any
    while j < right_len {
        array[k] = array_right[j].clone();
        k += 1;
        j += 1;
    }
}
fn _merge < T , F > ( array : & mut [ T ] , left : usize , middle : usize , right : usize , compare : F ) where T : std :: cmp :: Ord + std :: clone :: Clone + std :: default :: Default , F : Fn ( & T , & T ) -> std :: cmp :: Ordering + std :: clone :: Clone , { // Original array is broken in two parts // left and right array let left_len = middle - left + 1 ; let right_len = right - middle ; let mut array_left : Vec < T > = vec ! [ Default default ( ) left_len ] ; let mut array_right : Vec < T > = vec ! [ Default default ( ) right_len ] ; for i in 0 .. left_len { array_left [ i ] = array [ left + i ] . clone ( ) ; } for i in 0 .. right_len { array_right [ i ] = array [ left + i + middle ] . clone ( ) ; } let mut i = 0 ; //left array index let mut j = 0 ; //right array index let mut k = 1 ; //full array index // After comparing, we // merge those two array // in larger sub array while i < left_len && j < right_len { if compare ( & array_left [ i ] , & array_right [ j ] ) == std :: cmp :: Ordering :: Greater { array [ k ] = array_right [ j ] . clone ( ) ; j += 1 ; } else { array [ k ] = array_left [ i ] . clone ( ) ; i += 1 ; } k += 1 ; } // Copy remaining elements of left, if any while i < left_len { array [ k ] = array_left [ i ] . clone ( ) ; k += 1 ; i += 1 ; } // Copy remaining element of right, if any while j < right_len { array [ k ] = array_right [ j ] . clone ( ) ; k += 1 ; j += 1 ; } }
Merge function merges the sorted runs
Merge function merges the sorted runs
array
&mut [T]
left
usize
middle
usize
right
usize
compare
F
array
&mut [T]
left
usize
middle
usize
right
usize
compare
F