Skip to content

Instantly share code, notes, and snippets.

@superddr
Last active December 14, 2015 12:09
Show Gist options
  • Save superddr/5084636 to your computer and use it in GitHub Desktop.
Save superddr/5084636 to your computer and use it in GitHub Desktop.
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