Merge sort

mail@pastecode.io avatar
unknown
rust
3 years ago
578 B
1
Indexable
Never
fn main {
    let a:Vec<i64> = vec![1, 5, 8];
    let b:Vec<64> = vec![2,4,7];
    println!({:?}, merge(a, b));
}

fn merge(a:Vec<i64>, b:Vec<i64>) -> Vec<i64> {
    let mut ai = a.iter();
    let mut bi = b.iter();
    let mut al = ai.last();
    let mut bl = bi.last();
    let mut result:Vec<i64> = vec![];
    loop {
        if let Some(i) =  (None, None) {break}
        else if (al, None) {al = ai.next()};
        else if (None, bl) {bl = bl.next()};
        else {if al > bl {al = ai.next(); ai.next();} else {bl = bi.next(); bi.next();};
        result.push(i);
    }
}