How exactly does Javascript's array.reverse() work? Does it go through and swap every element of the array? If so, does it take O(n) to swap an array of size n?
I guess the reason I am asking is because I was wondering if array.reverse() was the same as:
for(var i = 0; i < a.length / 2; i++) {
var holder = a[i];
a[i] = a[a.length - 1 - i];
a[a.length - 1 - i] = holder;
}
NOTE: Sorry if the Javascript code I posted is incorrect, it's pretty late right now.
EDIT: Fixed a.length to a.length / 2.
a.length / 2(integer division of a.length and 2)