Skip to content

Instantly share code, notes, and snippets.

@robinhouston
Created September 26, 2011 20:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robinhouston/1243365 to your computer and use it in GitHub Desktop.
Save robinhouston/1243365 to your computer and use it in GitHub Desktop.
Mergesort implementation for http://visualsort.appspot.com/
merge = (start, len1, len2) ->
# merge the slice [start, start+len1) with the slice [start+len1, start+len2)
i = start; j = start+len1
while i < j and j < start+len2
VA.insert(j++, i) if VA.lt(j, i)
i++
mergesort = (start, len) ->
# sort the slice [start, start+len)
VA.highlight([start ... start+len])
return if len < 2
mid = Math.round(len / 2)
mergesort(start, mid)
mergesort(start+mid, len-mid)
merge(start, mid, len)
mergesort(0, VA.length)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment