Skip to content

Instantly share code, notes, and snippets.

@josephok
Last active August 29, 2015 13:58
Show Gist options
  • Save josephok/10341784 to your computer and use it in GitHub Desktop.
Save josephok/10341784 to your computer and use it in GitHub Desktop.
quick sort
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