сравнение алгоритмов мержа массивов (v3)

Revision 3 of this benchmark created on


Setup


const a = [
    24,  364,  394,  415,  492,  502,  555,  605,  724,  762,
   975,  983, 1156, 1265, 1367, 1463, 1523, 1700, 1725, 1837,
  1849, 1873, 1970, 2180, 2438, 2506, 2514, 2665, 2788, 2829,
  2932, 2956, 3183, 3260, 3275, 3282, 3335, 3441, 3533, 3535,
  3732, 3810, 3876, 4088, 4090, 4335, 4344, 4353, 4358, 4583,
  4671, 4711, 4884, 4989, 5204, 5243, 5270, 5341, 5970, 6018,
  6099, 6118, 6146, 6184, 6250, 6420, 6456, 6577, 6946, 7002,
  7016, 7283, 7364, 7398, 7752, 7773, 7784, 7877, 7923, 7983,
  8269, 8378, 8390, 8411, 8429, 8720, 8965, 8976, 9090, 9095,
  9198, 9230, 9247, 9283, 9428, 9465, 9505, 9551, 9676, 9983
];
const b = [
    45,  118,  128,  280,  297,  322,  331,  409,  525,  601,
   633,  708,  728,  902,  947,  989, 1037, 1483, 1584, 1766,
  1845, 1996, 2251, 2266, 2280, 2425, 2600, 2739, 2744, 2843,
  2939, 2964, 3133, 3165, 3194, 3197, 3248, 3286, 3383, 3511,
  3559, 3756, 3763, 3847, 3885, 3994, 4071, 4072, 4141, 4292,
  4513, 4582, 4638, 4685, 4784, 5075, 5211, 5344, 5350, 5471,
  5963, 6024, 6085, 6403, 6524, 6632, 6659, 6746, 6756, 6807,
  6923, 7005, 7100, 7118, 7291, 7375, 7422, 7679, 7842, 7976,
  8027, 8093, 8147, 8221, 8262, 8497, 8614, 8695, 8841, 8887,
  9314, 9470, 9478, 9556, 9683, 9745, 9781, 9911, 9937, 9989
];
const c = [];

Test runner

Ready to run.

Testing in
TestOps/sec
мой
let countA = 0;
let countB = 0;
let allCount = a.length + b.length;

while (countA + countB < allCount) {
  if (a[countA] > b[countB]) {
    countB < b.length ? c.push(b[countB++]) : c.push(a[countA++]);
  } else {
    countA < a.length ? c.push(a[countA++]) : c.push(b[countB++]);
  }
}

console.log(c);
ready
Светлана
for (let i = 0; i < a.length; i++) {
    c[c.length] = a[i];
}
for (let i = 0; i < b.length; i++) {
    c[c.length] = b[i];
}
 
for (let i = 0; i < c.length - 1; i++) {
    for (let j = 0; j < c.length - i - 1; j++) {
        if (c[j] > c[j + 1]) {
            let temp = c[j];
            c[j] = c[j + 1];
            c[j + 1] = temp;
        }
    }
}
 
console.log(c);
ready
София
// создаем индексы, по которым будем бежать
let indexArrA = 0, indexArrB = 0;
 
// начинаем идти по массивам
while (indexArrA < a.length && indexArrB < b.length) {
// какое значение меньше, то и добавляем в массив
// и увеличиваем индекс, потому что больше нам это значение сравнивать не нужно
    if (a[indexArrA] < b[indexArrB]) {
        c.push(a[indexArrA]);
        indexArrA++;
    } else {
        c.push(b[indexArrB]);
        indexArrB++;
    }
}
 
// Добавляем остатки или из a, или из b
while ( indexArrA < a.length) {
    c.push(a[ indexArrA]);
    indexArrA++;
}
while (indexArrB < b.length) {
    c.push(b[indexArrB]);
   indexArrB++;
}

console.log(c)
ready
Найыр
function mergeArr(arr1, arr2) {
  const result = [];
  let i = 0;
  let j = 0;
 
  while (i < arr1.length || j < arr2.length) {
    const value1 = arr1[i]; 
    const value2 = arr2[j];
 
    if (i === arr1.length || (j < arr2.length && value2 < value1)) {
      result.push(value2);
      j++;
    } else {
      result.push(value1);
      i++;
    }
  }
 
  return result;
}
console.log(c = mergeArr(arr1,arr2))
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.