Skip to content

Instantly share code, notes, and snippets.

@unilecs
Created October 14, 2017 08:09
Show Gist options
  • Save unilecs/11ee99a1e9ed1b5f381e0fb5392959a5 to your computer and use it in GitHub Desktop.
Save unilecs/11ee99a1e9ed1b5f381e0fb5392959a5 to your computer and use it in GitHub Desktop.
Перестановка четных/нечетных элементов в массиве (@MrMeison)
const sort = (arr) => {
let i = 0;
let j = arr.length - 1;
for(;i < j;) {
if (!(arr[i] % 2)) {
i++;
}
if (arr[j] % 2) {
j--;
}
if (arr[i] % 2 && !(arr[j] % 2)) {
const temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
i++;
j--;
}
}
return [...arr];
}
console.log(sort([1,2,3,4,5,6])); // 6 2 4 3 5 1
console.log(sort([1,7,5,3,9])); // 1 7 5 3 9
console.log(sort([1,3,5,6,9,8])); // 8 6 5 3 9 1
console.log(sort([6])); // 6
console.log(sort([])); // []
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment