Last active
August 29, 2015 13:58
-
-
Save josephok/10341784 to your computer and use it in GitHub Desktop.
quick sort
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
quicksort = (array) -> | |
_quicksort = (array, left, right) -> | |
[i, j] = [left, right] | |
if left > right | |
return | |
pivot = array[left] | |
until i is j | |
while array[j] >= pivot and i < j | |
j-- | |
while array[i] <= pivot and i < j | |
i++ | |
if i < j | |
[array[i], array[j]] = [array[j], array[i]] | |
[array[left], array[i]] = [array[i], array[left]] | |
_quicksort(array, left, i - 1) | |
_quicksort(array, i + 1, right) | |
_quicksort(array, 0, array.length - 1) | |
return array | |
console.log quicksort([5, 55, 56, 99, 100, 95, -98, 4, 9, 8, 7, 0, -9, 111]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment