Merge two arrays and sort the final one

前端 未结 11 818
情歌与酒
情歌与酒 2021-01-15 17:42

In an interview I was asked the following question. I am given two arrays, both of them are sorted.

BUT

Array 1 will have few -1\'s and Array 2 will have to

相关标签:
11条回答
  • 2021-01-15 18:26

    Since they are both sorted, the order of arrayTwo's items should match the order of -1s in arrayOne. Then the job becomes simple and can be implemented as follows;

    function replaceMissing(a,b){
      var i = 0;
      return a.map(n => n < 0 ? b[i++] : n);
    }
    var arrayOne = [3,6,-1,11,15,-1,23,34,-1,42],
        arrayTwo = [7,19,38];
          result = replaceMissing(arrayOne,arrayTwo);
    console.log(result);

    Edit: I believe the upper solution does make more sense in the general logic of the question. If the position of -1s does not mean anything then what use do they have? Let's just delete the -1's and do a simple insertion of arrayTwo items at proper indices in arrayOne. This can very simply be done as follows.

    function replaceMissing(a,b){
      var j = b.length-1;
      return b.concat(a.reduceRight((r,m,i) => (m < 0 ? r.splice(i,1)
                                                      : m < b[j] && r.splice(i+1,0,b.splice(j--,1)[0]),
                                                r), a.slice()));
    }
    var arrayOne = [3,6,-1,11,15,-1,23,34,-1,42],
        arrayTwo = [1,25,38];
          result = replaceMissing(arrayOne,arrayTwo);
    console.log(result);

    0 讨论(0)
  • 2021-01-15 18:27

    That's actually an interesting question. There are many sorting algorithms, and the most efficient ones always starts from one "unchangeable" array, so without changing the values inside that. Yet here your goal is to change the value when it encounters -1, so that the value is taken from the second array.

    So, you need a sorting algorithm that doesn't divide the array in pieces because if the last element of your second array is 1 (the lowest), it has to be moved to the start. If you're using a sorting algorithm that breaks the array in pieces (the divide-and-conquer tactic like quick sort) or that uses recursion, it can be problematic because it cannot be moved to the start of your main array. Unless you are aware of the main array.

    What you need is an algorithm that performs a step-by-step algorithm.

    The algorithm that I've used is a bubble sort, which checks each element step by step. It's then easier to replace the value if it's -1 and move its position correctly to the array. However, it is not so efficient. Maybe I will edit my post to see if I can improve that.

    function mergeAndSort(arr1, arrMergeIn) {
      // merge + sort using arr1 as large one
      var mergeIndex = 0;
    
      for (var i = 0; i < arr1.length; ++i) {
        if (arr1[i] === -1) arr1[i] = arrMergeIn[mergeIndex++];
        var j = i;
        while (j > 0 && arr1[j - 1] > arr1[j]) {
          var tmp = arr1[j - 1];
          arr1[j - 1] = arr1[j];
          arr1[j] = tmp;
          j--
        }
      }
      return arr1;
    }
    
    // one liner console output
    function showArray(arr) {
    console.log(arr.join(','));
    }
    
    
    showArray(mergeAndSort([3, 6, -1, 11, 15, -1, 23, 34, -1, 42], [7, 19, 38]));
    
    showArray(mergeAndSort([3, 36, -1, 1, 10, -1, 9, 34, -1, 42], [17, 9, 38]));
    
    showArray(mergeAndSort([3, 36, -1, 1, 10, -1, 9, 34, -1, 42], [17, 9, 1]));
    
    showArray(mergeAndSort([-1, 36, -1, 1, 10, -1, 9, 34, -1, 42], [17, 9, 100, 1]));
    
    showArray(mergeAndSort([-1, -1, 1, 100, -1, 9, 34, -1], [17, 9, 9, 1]));

    Or, you can use another strategy: replace the "-1" elements with the elements from that another array and perform an efficient algorithm on that. Although in worst-case scenario's, the "-1"s are at the end of the array, which means that there is an ~N operation + additional average complexity of a sorting algorithm (the efficient ones are of ~N*log(N))

    0 讨论(0)
  • 2021-01-15 18:27

    You could iterate arrayOne in a single loop and then arrayTwo.

    The idea is to separate the target index from the actual index. The first loop ignores -1 and and keep the target index.

    If an actual value is greater then the first value of arrayTwo, both values swapped and in arrayTwo takes a sorting place by iterating and swapping with grater values.

    Then the actual item is assigned to the target index.

    Both indices gets incremented.

    At the end all items of arrayTwo are added to arrayOne.

    function order(arrayOne, arrayTwo) {
        var i = 0, j, l = 0;
        while (i < arrayOne.length) {
            if (arrayOne[i] === -1) {
                i++;
                continue;
            }
            if (arrayTwo[0] < arrayOne[i]) {
                [arrayOne[i], arrayTwo[0]] = [arrayTwo[0], arrayOne[i]];
                j = 0;
                while (arrayTwo[j] > arrayTwo[j + 1]) {
                    [arrayTwo[j], arrayTwo[j + 1]] = [arrayTwo[j + 1], arrayTwo[j]];
                    j++;
                }
            }
            arrayOne[l++] = arrayOne[i++];
        }
        j = 0;
        while (l < arrayOne.length) {
            arrayOne[l++] = arrayTwo[j++];
        }
        return arrayOne;
    }
    
    console.log(order([3, 6, -1, 11, 15, -1, 23, 34, -1, 42], [7, 19, 38]));
    console.log(order([3, 6, -1, 11, 15, -1, 23, 34, -1, 42], [1, 9, 28]));
    console.log(order([3, 6, -1, 11, 15, -1, 23, 34, -1, 42], [1, 2, 5]));
    console.log(order([3, 6, -1, 11, 15, -1, 23, 34, -1, 42], [43, 44, 45]));
    .as-console-wrapper { max-height: 100% !important; top: 0; }

    0 讨论(0)
  • 2021-01-15 18:31

    Here is a simple and compact implementation of the merge sort algorithm. There will only be the same amount of operations as there are elements.

    I achieve this by creating an array with the same number of elements as both arrays combined, then iterating that array.

    Upon each iteration:

    • If both arrays have elements (and the smallest element is not -1) it will move the smallest element into the result.
    • If only one of the arrays still has elements (and the next element is not -1), it will move the first element from that array into the result.

    Finally assign the result to the first array as per your spec

    const mergeSort = (a, b) => Object.assign(a, 
      new Int32Array(a.length + b.length).reduce(m => {
        const el = [
          [a, b][+!(a[0] <= b[0])], 
          [a, b][+!a.length]][+!(a.length && b.length)
        ].shift()
        if(el !== -1) m.push(el)
        return m
      }, [])
    )
    
    const arr1 = [1,3,5,7,9]
    const arr2 = [0,2,4]
    
    mergeSort(arr1, arr2)
    
    console.log(arr1) // [0,1,2,3,4,5,7,9]

    0 讨论(0)
  • 2021-01-15 18:37

    function merger(a1, a2) {
      var i = 0;
      var j = 0;
    
      while (i < a1.length && j < a2.length) {
        if (a1[i] > a2[j]) {
          // Swap values
          var temp = a2[j];
          a2[j] = a1[i];
          a1[i] = temp;
          i++;
        } else if (a1[i] !== -1 && a1[i] <= a2[j]) {
          i++;
        } else {
          var temp = a2[j];
          a2[j] = a1[i];
          a1[i] = temp;
          i++;
          j++;
        }
      }
    
      return a1;
    }
    
    var arrayOne = [3, 5, -1, 11, 15, -1, 23, 34, -1, 42];
    var arrayTwo = [6, 19, 38];
    
    console.log(merger(arrayOne, arrayTwo))

    With certain pre-conditions (no other numbers with <0, a1 should be smaller than a2 etc - which could all be handled) this should solve the problem in JS.

    0 讨论(0)
提交回复
热议问题