Skip to content

Instantly share code, notes, and snippets.

@dannylagrouw
Created February 4, 2010 21:18
Show Gist options
  • Save dannylagrouw/295118 to your computer and use it in GitHub Desktop.
Save dannylagrouw/295118 to your computer and use it in GitHub Desktop.
def split[A <% Ordered[A]](list: List[A], p: A): (List[A], List[A]) = list match {
case e :: rest =>
val (left, right) = split(rest, p)
if (e < p)
(e :: left, right)
else
(left, e :: right)
case _ => (Nil, Nil)
}
def qsort[A <% Ordered[A]](list: List[A]): List[A] = list match {
case p :: rest =>
val (left, right) = split(rest, p)
qsort(left) ++ (p :: qsort(right))
case _ => Nil
}
println(qsort(List(1241, 1, 234, 503, 122, 99, 10, 1992, 1200)))
println(qsort("quicksort".toList))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment