Skip to content

Instantly share code, notes, and snippets.

@Yaneeve
Last active December 13, 2018 10:43
Show Gist options
  • Save Yaneeve/b5cda3dd1a5f3974c95743c0b99c1ce8 to your computer and use it in GitHub Desktop.
Save Yaneeve/b5cda3dd1a5f3974c95743c0b99c1ce8 to your computer and use it in GitHub Desktop.
quicksort à la LYAHFGG
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