Skip to content

Instantly share code, notes, and snippets.

@7shi
Last active September 12, 2016 17:33
Show Gist options
  • Save 7shi/7e0598f0b08c222494ec to your computer and use it in GitHub Desktop.
Save 7shi/7e0598f0b08c222494ec to your computer and use it in GitHub Desktop.
[Scala][Haskell]quicksort
qsort [] = []
qsort (n:xs) = qsort lt ++ [n] ++ qsort gteq where
lt = [x | x <- xs, x < n]
gteq = [x | x <- xs, x >= n]
def qsort[A <% Ordered[A]](list: List[A]): List[A] = list match {
case Nil => Nil
case (n :: xs) => {
val lt = for (x <- xs if x < n) yield x
val gteq = for (x <- xs if x >= n) yield x
qsort(lt) ++ List(n) ++ qsort(gteq)
}
}
qsort(List(2,1,5,4,3))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment