Last active
June 5, 2018 22:03
-
-
Save kulicuu/054f3ac161c974cee53ea80b6e02cfc4 to your computer and use it in GitHub Desktop.
Quicksort in CoffeeScript
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
| exports.quicksort = quicksort = ({ rayy }) -> | |
| len = rayy.length | |
| if (len is 1) or (len is 0) then return rayy | |
| rand_idx = Math.floor(Math.random() * len) | |
| [pivot] = rayy.splice rand_idx, 1 | |
| less = [] | |
| more = [] | |
| for num, idx in rayy | |
| if num < pivot | |
| less.push num | |
| else | |
| more.push num | |
| less_sorted = quicksort { rayy: less } | |
| more_sorted = quicksort { rayy: more } | |
| less_sorted.concat pivot, more_sorted |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment