Skip to content

Instantly share code, notes, and snippets.

@calvinlfer
Last active May 7, 2019 20:54
Show Gist options
  • Select an option

  • Save calvinlfer/c059bb27cbaa9ec54114b745b36fd740 to your computer and use it in GitHub Desktop.

Select an option

Save calvinlfer/c059bb27cbaa9ec54114b745b36fd740 to your computer and use it in GitHub Desktop.
Generic Quick Sort in Scala
def quickSort[T](list: List[T])(implicit o: Ordering[T]): List[T] =
list match {
case Nil => Nil
case (head :: tail) =>
// select head as the pivot point
val pivot = head
val (lessThanPivot, greaterThanPivot) = tail.partition(e => o.lteq(pivot, e))
quickSort(lessThanPivot) ::: pivot :: quickSort(greaterThanPivot)
}
@martinprobson
Copy link

Thanks, was looking for a nice implementation of quicksort in scala to compare to the classic Haskell version. Now I just need to work out how to make it tail recursive...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment