Created
August 20, 2015 16:30
-
-
Save odesskij/3d0ddfa7fbd17f84e635 to your computer and use it in GitHub Desktop.
qsort with coffee
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
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