Skip to content

Instantly share code, notes, and snippets.

@batogov
Created September 24, 2017 19:55
Show Gist options
  • Save batogov/690f071c5e14fcf18241548b9829a898 to your computer and use it in GitHub Desktop.
Save batogov/690f071c5e14fcf18241548b9829a898 to your computer and use it in GitHub Desktop.
Sort odd and even
var magicSort = function(arr) {
var temp = 0;
for (var i = 0; i < arr.length; i++) {
for (var j = 0; j < arr.length - 2; j++) {
if (j % 2 === 0 && arr[j] < arr[j + 2]) {
temp = arr[j];
arr[j] = arr[j + 2];
arr[j + 2] = temp;
} else if (j % 2 === 1 && arr[j] > arr[j + 2]) {
temp = arr[j];
arr[j] = arr[j + 2];
arr[j + 2] = temp;
}
}
}
return arr;
}
console.log(magicSort([1, 2, 5, 8, 12, 13, 16, 17]));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment