Last active
July 5, 2016 06:55
-
-
Save wesalvaro/f52a71f2fddc0cb772f4ebb6006afee0 to your computer and use it in GitHub Desktop.
A simple implementation of wiggle sorting.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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