Skip to content

Instantly share code, notes, and snippets.

@kmizu
Created May 16, 2010 13:16
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 kmizu/402873 to your computer and use it in GitHub Desktop.
Save kmizu/402873 to your computer and use it in GitHub Desktop.
def sort(src: List[Int]): List[Int] = src match {
case (Nil|_::Nil) => src
case _ =>
def merge(l: List[Int], r: List[Int]): List[Int] = (l, r) match {
case (Nil, ys) => ys
case (xs, Nil) => xs
case (x::xs, y::ys) => if(x < y) x::merge(xs, r) else y::merge(l, ys)
}
val (l, r) = src.splitAt(src.length / 2)
merge(sort(l), sort(r))
}
val list = List(8,4,3,7,6,5,2,1)
println(sort(list))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment