Advertisement
nher1625

Swap_Algorithm_Analysis

May 29th, 2015
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Swapping Algorithms that may be more or less efficient depending on the browser.
  2. // First variation: (Temporary Swap)
  3. function temporarySwap(array) {
  4.     var left = null;
  5.     var right = null;
  6.     var length = array.length;
  7.     for (left = 0, right = (length - 1); left < right; left += 1, right -= 1) {
  8.         var temporary = array[left];
  9.         array[left] = array[right];
  10.         array[right] = temporary;
  11.     }
  12.     return array;
  13. }
  14. /*
  15. [1,2,3]
  16.  
  17. (1) variables representing both extremes of the array are instantiated.
  18. (2) the total array length is instantiated.
  19. (3) A loop is entered on the conditions:
  20.     (A) left is 0 and right is the last index of the array
  21.     (B) left < right
  22.     (C) if (B) is true then the left end will increase by 1 and the right end will decrease by 1
  23. (first iteration)
  24.     temp is instantiated as a temporary placeholder of the leftmost extreme of the array
  25.     leftmost becomes the rightmost
  26.     rightmost becomes the placeholder that contains the leftmost
  27. (4) Iterations accomplished until there is full convergance and left < right != true
  28. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement