Skip to content

Instantly share code, notes, and snippets.

@yefim
Last active December 14, 2015 06:29
Show Gist options
  • Save yefim/5043290 to your computer and use it in GitHub Desktop.
Save yefim/5043290 to your computer and use it in GitHub Desktop.
Merge Sort implemented in CoffeeScript
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