Skip to content Skip to sidebar Skip to footer

Reverse Array In Place

Why won't this function reverseArrayInPlace work? I want to do simply what the function says - reverse the order of elements so that the results end up in the same array arr. I am

Solution 1:

Here's a simpler way of reversing an array, using an in-place algorithm

functionreverse (array) {
  var i = 0,
      n = array.length,
      middle = Math.floor(n / 2),
      temp = null;

  for (; i < middle; i += 1) {
     temp = array[i];
     array[i] = array[n - 1 - i];
     array[n - 1 - i] = temp;
  }
}

You "split" the array in half. Well, not really, you just iterate over the first half. Then, you find the index which is symmetric to the current index relative to the middle, using the formula n - 1 - i, where i is the current index. Then you swap the elements using a temp variable. The formula is correct, because it will swap:

0 <-> n - 11 <-> n - 2

and so on. If the number of elements is odd, the middle position will not be affected.

Solution 2:

pop() will remove the last element of the array, and push() will append an item to the end of the array. So you're repeatedly popping and pushing just the last element of the array.

Rather than using push, you can use splice, which lets you insert an item at a specific position in an array:

var reverseArrayInPlace = function (array) {
    var arrLength = array.length;
    for (i = 0; i < arrLength; i++) {
        array.splice(i, 0, array.pop());
    }
}

(Note that you don't need the intermediate array to do this. Using an intermediate array isn't actually an in-place reverse. Just pop and insert at the current index.)

Also, interesting comment -- you can skip the last iteration since the first element will always end up in the last position after length - 1 iterations. So you can iterate up to arrLength - 1 times safely.

I'd also like to add that Javascript has a built in reverse() method on arrays. So ["a", "b", "c"].reverse() will yield ["c", "b", "a"].

A truly in-place algorithm will perform a swap up to the middle of the array with the corresponding element on the other side:

var reverseArrayInPlace = function (array) {
    var arrLength = array.length;
    for (var i = 0; i < arrLength/2; i++) {
        var temp = array[i];
        array[i] = array[arrLength - 1 - i];
        array[arrLength - 1 - i] = temp;
    }
}

Solution 3:

If you are doing Eloquent Javascript, the exercise clearly states to not use a new array for temporary value storage. The clues in the back of the book present the structure of the solution, which are like Stefan Baiu's answer.

My answer posted here uses less lines than Stefan's since I think it's redundant to store values like array.length in variables inside a function. It also makes it easier to read for us beginners.

functionreverseArrayInPlace(array) {

    for (var z = 0; z < Math.floor(array.length / 2); z++) {

        var temp = array[z];
        array[z] = array[array.length-1-z];
        array[array.length-1-z] = temp;

    }

    returnarray;
}

Solution 4:

I think you want a simple way to reverse an array. Hope it will help you

var yourArray = ["first", "second", "third", "...", "etc"]
var reverseArray = yourArray.slice().reverse()

console.log(reverseArray)

You will get

["etc", "...", "third", "second", "first"]

Solution 5:

You are calling the function with arr as parameter, so both arr and array refer to the same array inside the function. That means that the code does the same as:

var arr = ["a","b","c","d","e","f"]
var arr2 = []

var arrLength = arr.length;
for (i = 0; i < arrLength; i++) {
  arr2.push(arr.pop())
  arr.push(arr2.shift())
}

The first statements get the last item from arr and places it last in arr2. Now you have:

arr = ["a","b","c","d","e"]
arr2 = ["f"]

The second statement gets the first (and only) item from arr2 and puts it last in arr:

arr = ["a","b","c","d","e","f"]
arr2 = []

Now you are back where you started, and the same thing happens for all iterations in the loop. The end result is that nothing has changed.


To use pop and push to place the items reversed in the other array, you can simply move the items until the array is empty:

while (arr.length > 0) {
  arr2.push(arr.pop());
}

If you want to move them back (instead of just using the new array), you use shift to get items from the beginning of arr2 and push to put them at the end of arr:

while (arr2.length > 0) {
  arr.push(arr2.shift());
}

Doing a reversal in place is not normally done using stack/queue operations, you just swap the items from the beginning with the items from the end. This is a lot faster, and you don't need another array as a buffer:

for (var i = 0, j = arr.length - 1; i < j; i++, j--) {
  var temp = arr[i];
  arr[i] = arr[j];
  arr[j] = temp;
}

This swaps the pairs like this:

["a","b","c","d","e"]
  |   |       |   |
  |   +-------+   |
  +---------------+

Post a Comment for "Reverse Array In Place"