Monday, September 29, 2008

Quicksort in Scala


def sort(list: List[Int]): List[Int] = {
list match {
case List() => list
case List(_) => list
case List(_, _*) => {
val pivot = list(list.length/2)
sort(list.filter(x => x < pivot)) ::: list.filter(x => x == pivot) ::: sort(list.filter(x => x > pivot))
}
}
}

No comments: