Last active
January 1, 2016 22:59
-
-
Save liamgriffiths/8213546 to your computer and use it in GitHub Desktop.
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
// quick sort | |
// https://en.wikipedia.org/wiki/Quicksort | |
var sort = function(list) { | |
if (list.length <= 1) return list; | |
var pivot = list.splice(Math.floor(list.length / 2), 1); | |
var less = [], greater = []; | |
while (list.length) { | |
var item = list.shift(); | |
if (item <= pivot) { | |
less.push(item); | |
} else { | |
greater.push(item); | |
} | |
} | |
return sort(less).concat(pivot).concat(sort(greater)); | |
}; | |
module.exports = sort; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment