1. 程式人生 > >兩個陣列值按遞增歸併

兩個陣列值按遞增歸併

a = [3, 5, 8, 11]

b = [2, 6, 8, 9, 11, 15, 20]

將 陣列a、b值按遞增歸併。得到 c = [2, 3, 5, 6, 8, 8, 9, 11, 11, 15, 20]

let a = [3, 5, 8, 11];
let b = [2, 6, 8, 9, 11, 15, 20];
let c = [];
let i = 0, j = 0, z = 0;

while(a[i] && b[j]){
    if(a[i] < b[j]){
        c[z] = a[i];
        i++;
    } else {
        c[z] = b[j];
        j++;
    }
    z++;
}

while(a[i]){
    c[z] = a[i];
    z++;
    i++;
}

while(b[j]){
    c[z] = b[j];
    z++;
    j++;
}