Created
May 24, 2011 20:49
-
-
Save balinterdi/989634 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| qsort = (coll) -> | |
| if coll.length <= 1 | |
| coll | |
| else | |
| less = [] | |
| more = [] | |
| idx = Math.floor(Math.random() * coll.length) | |
| pivot = coll.splice idx, 1 | |
| for elt in coll | |
| if elt < pivot[0] then less.push(elt) else more.push(elt) | |
| Array.prototype.concat.call(qsort(less), pivot, qsort(more)) | |
| console.log qsort [4, 14, -3, 1, 2, 34, 7, -2, 0] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment