Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Swapping Algorithms that may be more or less efficient depending on the browser.
- // First variation: (Temporary Swap)
- function temporarySwap(array) {
- var left = null;
- var right = null;
- var length = array.length;
- for (left = 0, right = (length - 1); left < right; left += 1, right -= 1) {
- var temporary = array[left];
- array[left] = array[right];
- array[right] = temporary;
- }
- return array;
- }
- /*
- [1,2,3]
- (1) variables representing both extremes of the array are instantiated.
- (2) the total array length is instantiated.
- (3) A loop is entered on the conditions:
- (A) left is 0 and right is the last index of the array
- (B) left < right
- (C) if (B) is true then the left end will increase by 1 and the right end will decrease by 1
- (first iteration)
- temp is instantiated as a temporary placeholder of the leftmost extreme of the array
- leftmost becomes the rightmost
- rightmost becomes the placeholder that contains the leftmost
- (4) Iterations accomplished until there is full convergance and left < right != true
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement