0

I was try to visualize a bubble sort with JS.I use a binary array to save each changed array.But in the binary array i only get array which has been sorted.

var arr = [1, 5, 3, 7, 2];
var newArray = [];

for (var i = 0; i < arr.length - 1; i++) {
  for (var j = 0; j < arr.length - 1 - i; j++) {
    if (arr[j] > arr[j + 1]) {
      arr[j] = arr[j] + arr[j + 1];
      arr[j + 1] = arr[j] - arr[j + 1];
      arr[j] = arr[j] - arr[j + 1];
      console.log(arr);
      newArray.push(arr);
    }
  }
}

I can't understand why i get the right result in the loop when i console arr[],but the newArray is [1, 2, 3, 5, 7],[1, 2, 3, 5, 7],[1, 2, 3, 5, 7],[1, 2, 3, 5, 7].

Why in the same scope the console and the push function get the different results?

How can i get the correct binary array?

i'm a newbie and completely confused about it.Does someone know the reason? Thank you

2
  • "Why in the same scope the console and the push function get the different results?" It doesn't. In each iteration you are changing arr. In JavaScript, arrays are reference types, i.e. the array is not copied when you pass it to a function or assign it to a variable. Here is a simple example to demonstrate the issue: var arr = [1,2,3]; console.log(arr); /*1,2,3*/ arr[0] = 4; console.log(arr); /*4,2,3*/. The second console.log shows a different array because I changed arr. Commented Apr 27, 2018 at 15:58
  • You are pushing a reference to the same array onto newArray. You end up with the same array multiple times in newArray so, of course, they are all identical. You need to copy each member into a new array and push that onto newArray Commented Apr 27, 2018 at 15:58

2 Answers 2

1

I'm not entirely clear on what exactly you are trying to do, but I think your idea is to have newArray contain a snapshot of the array at each step of the sort. Your code doesn't work because you are pushing the same array into newArray at each step. You need to make a copy of the current arr at each step. Luckily, this can be easily done with slice:

        var arr = [1,5,3,7,2];
        var newArray = [];

        for (var i = 0; i < arr.length - 1; i++) {            
            for (var j = 0; j < arr.length - 1 - i; j++) {
                if (arr[j] > arr[j + 1]) {
                    arr[j]   = arr[j] + arr[j+1];
                    arr[j+1] = arr[j] - arr[j+1];
                    arr[j]   = arr[j] - arr[j+1];
                    //console.log(arr);
                    newArray.push(arr.slice(0,arr.length));
                }
            }
        }
console.log(newArray);
console.log(arr);
        

Now newArray is an array of array containing a snapshot of the array at each step of the sort.

Sign up to request clarification or add additional context in comments.

Comments

0

This problem occurs normally because .push() passes a pointer to arr. Therefore, newArray stores 4 pointers to arr. To solve this problem, you should, like the answer above does, pass a copy of arr each time.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.