0

in merging process compare number of both array and push in new array and remove from original array. use while loop for this process.my wrong program is:

function mergeSortedArray(a, b){
  var temp=[];
  var i=0;
  var j=0;
  while(a[i]||b[j]){
    if(a[i]<b[j]){
      temp.push(a[i])
      i++;
    }
    else{
      temp.push(b[j]);
      j++;
    }

      }
      return temp;
}

  

console.log(mergeSortedArray([1,2,3,5,8],[1,2,4,6,9,10]));
Amit Modi
  • 109
  • 1
  • 1
  • 7
  • i used pointer in this code. – Amit Modi Aug 15 '15 at 04:01
  • Why don't you try debugging your code? Step through it line by line to see what it is doing. Also, look at your code closely and think about what it is doing, especially when it reaches the end of one array or the other. –  Aug 15 '15 at 04:07
  • Your wrong code is working good on my end. What is the problem ? – Frederik.L Aug 15 '15 at 04:27
  • he hasn't even written the whole question yet – Jaromanda X Aug 15 '15 at 04:29
  • duplicated HIS OWN QUESTION - http://stackoverflow.com/questions/31922223/how-to-merge-two-sorted-array-in-one-sorted-array-in-javascript-without-using-so – Jaromanda X Aug 15 '15 at 04:33
  • here pointer is not like C language pointer – Amit Modi Aug 15 '15 at 04:34
  • `var a = [3, 9, 34, 198, 200, 203, 222, 777]; var b =[5, 6, 8,9,12, 34,78]; var arr = []; var i = 0; var j = 0; var totalsize = a.length + b.length; var temp = 0; while(temp < totalsize) { if ((a[i] < b[j])||(a[i]==b[j])){ arr.push(a[i]); i++; }else { arr.push(b[j]); j++; } temp++; } console.log('final : '+arr);` [mergeSort](http://www.stoimen.com/blog/2010/07/02/friday-algorithms-javascript-merge-sort/) – Yash Aug 15 '15 at 04:37
  • @AmitModi There is no pointer in javascript. What you probably mean is `iterators`, which is using pointers under the hood but you won't ever touch them directly. – Frederik.L Aug 15 '15 at 04:38
  • modify my code for remove number from original array and add to the new array in sorted order. – Amit Modi Aug 15 '15 at 04:53
  • @Frederik.L, in any language you can use a varible as a pointer. For example in javascript you can make a variable that *points* to the currently processed index of an array. It would be fair to call such a variable a pointer. In programming, the word pointer *also* has a narrow and specific meaning that you are referring to, but it's not the meaning that was used here. – Andrew Savinykh Aug 19 '15 at 20:11

0 Answers0