-
-
Save superddr/5084636 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
var arr = [1, 2, 3, 4, 5, 6, 7,8,9,10,11,12,13,14,15,16]; | |
var ver = [5, 3, 9, 6, 8, 2, 4]; | |
document.writeln(arr + "<br/> "); | |
var l = 0, r = 1; | |
while (r < arr.length) { | |
if (isEven(l) && isOdd(r)) { | |
swap(l, r); | |
l++; r++; | |
} else if (isEven(l)) { | |
swap(l, r); | |
r++; | |
} else if (isOdd(l) && isOdd(r)) { | |
l++; | |
} else if (isOdd(l)) { | |
l++; | |
} | |
} | |
//document.writeln(arr); | |
function isOdd(i) | |
{ | |
return arr[i] % 2 == 1; | |
} | |
function isEven(i) | |
{ | |
return arr[i] % 2 == 0; | |
} | |
function swap(i, j) | |
{ | |
var t = arr[i]; | |
arr[i] = arr[j]; | |
arr[j] = t; | |
document.writeln(arr + "<br/> "); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment