Last active
May 7, 2019 20:54
-
-
Save calvinlfer/c059bb27cbaa9ec54114b745b36fd740 to your computer and use it in GitHub Desktop.
Generic Quick Sort in Scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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...