Skip to content

Instantly share code, notes, and snippets.

@odesskij
Created August 20, 2015 16:30
Show Gist options
  • Save odesskij/3d0ddfa7fbd17f84e635 to your computer and use it in GitHub Desktop.
Save odesskij/3d0ddfa7fbd17f84e635 to your computer and use it in GitHub Desktop.
qsort with coffee
do () ->
array = (Math.round 100*Math.random() for i in [0..9])
console.log array, '<--- input'
qsort = (array) ->
sort = (array, left, right) ->
l = left
r = right
mid = array[Math.round((l + r)/2)]
while l <= r
while array[l] < mid and l <= right
l++
while array[r] > mid and r >= left
r--
if l <= r
foo = array[l]
array[l] = array[r]
array[r] = foo
l++
r--
if r > left then sort array, left, r
if l < right then sort array, l, right
return array
sort array, 0, (array.length - 1)
console.log (qsort array), '<---- output'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment