Skip to content

Instantly share code, notes, and snippets.

@larryv
Created June 2, 2014 19:51
Show Gist options
  • Save larryv/8e7d036571a4a02bd0ae to your computer and use it in GitHub Desktop.
Save larryv/8e7d036571a4a02bd0ae to your computer and use it in GitHub Desktop.
def sortCodeTrees[T <: CodeTree](trees: List[T]): List[T] = {
val pivot = trees.length / 2
if (pivot == 0) trees
else {
def merge(xs: List[T], ys: List[T]): List[T] = (xs, ys) match {
case (Nil, ys) => ys
case (xs, Nil) => xs
case (x :: xs1, y :: ys1) =>
if (weight(x) < weight(y)) x :: merge(xs1, ys)
else y :: merge(xs, ys1)
}
val (front, back) = trees splitAt pivot
merge(sortCodeTrees(front), sortCodeTrees(back))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment