Last active
December 13, 2018 10:43
-
-
Save Yaneeve/b5cda3dd1a5f3974c95743c0b99c1ce8 to your computer and use it in GitHub Desktop.
quicksort à la LYAHFGG
This file contains 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
import scala.math.Ordering | |
import scala.math.Ordering.Implicits._ | |
// https://scastie.scala-lang.org/Yaneeve/msPXA2ThRMqbi8fP6ODl2w | |
def quicksort[A: Ordering](xs: List[A]): List[A] = xs match { | |
case Nil => Nil | |
case h :: t => | |
val smallerThanHead = quicksort(t.filter(_ <= h)) | |
val greaterThanHead = quicksort(t.filter(_ > h)) | |
smallerThanHead ::: h :: greaterThanHead | |
} | |
quicksort(List(10,2,5,3,1,6,7,4,2,3,4,8,9)) | |
quicksort("the quick brown fox jumps over the lazy dog".toList).mkString |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment