Last active
December 14, 2015 06:29
-
-
Save yefim/5043290 to your computer and use it in GitHub Desktop.
Merge Sort implemented in CoffeeScript
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
merge = (a, p, q, l) -> | |
L = a[p..q] | |
R = a[q+1..l] | |
L.push Infinity | |
R.push Infinity | |
i = 0 | |
j = 0 | |
for k in [p..l] | |
if L[i] <= R[j] | |
a[k] = L[i] | |
i++ | |
else | |
a[k] = R[j] | |
j++ | |
return a | |
sort = (a, p, l) -> | |
p = 0 if !p? | |
l = a.length-1 if !l? | |
if p < l | |
q = Math.floor (p+l)/2 | |
sort a, p, q | |
sort a, q+1, l | |
merge a, p, q, l |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment