Created
August 5, 2014 19:48
-
-
Save kosperera/d1e9a6c041c4d6d26de2 to your computer and use it in GitHub Desktop.
Reverse an array without using another array. This method is called Temporary Swap and it only runs for half of the array.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function reverse(arr) | |
{ | |
var left = null; | |
var right = null; | |
var length = arr.length; | |
for (left = 0; left < length / 2; left += 1) | |
{ | |
right = length - 1 - left; | |
var temporary = arr[left]; | |
arr[left] = arr[right]; | |
arr[right] = temporary; | |
} | |
return arr; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment