Skip to content

Instantly share code, notes, and snippets.

@beyondlimits
Created May 2, 2019 07:58
Show Gist options
  • Select an option

  • Save beyondlimits/42cb48d48799759520a66d31dd0cd15c to your computer and use it in GitHub Desktop.

Select an option

Save beyondlimits/42cb48d48799759520a66d31dd0cd15c to your computer and use it in GitHub Desktop.
var arr = 'abcdefghijklmnopqrstivwxyz'.split('');
var start = 0;
var end = arr.length - 1;
var swaps = 0;
while (start < end) {
for (var i = start; i < end; i += 2) {
var tmp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = tmp;
swaps++;
}
start++;
end--;
}
console.log(arr); // b d f h j l n p r t v x z a c e g i k m o q s i w y
console.log(swaps); // 91
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment