Skip to content

Instantly share code, notes, and snippets.

@wesalvaro
Last active July 5, 2016 06:55
Show Gist options
  • Save wesalvaro/f52a71f2fddc0cb772f4ebb6006afee0 to your computer and use it in GitHub Desktop.
Save wesalvaro/f52a71f2fddc0cb772f4ebb6006afee0 to your computer and use it in GitHub Desktop.
A simple implementation of wiggle sorting.
Array.prototype.wiggleSort = function() {
for (let i = 0; i < this.length - 1; ++i) {
const shouldNotBeLessThan = i % 2;
const isLessThan = this[i] < this[i + 1];
if (shouldNotBeLessThan && isLessThan) {
[this[i], this[i + 1]] = [this[i + 1], this[i]];
}
}
return this;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment