Skip to content

Instantly share code, notes, and snippets.

@nlw0
Created May 5, 2017 18:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nlw0/ab6ff9bcb75d36501f09ef13619dd81f to your computer and use it in GitHub Desktop.
Save nlw0/ab6ff9bcb75d36501f09ef13619dd81f to your computer and use it in GitHub Desktop.
Quicksort in Scala, for my blog
object Quicksort extends App {
val inputStream = io.Source.fromInputStream(System.in)
val data = inputStream.getLines().toStream map (_.toInt)
quicksort(data) foreach { println(_) }
def quicksort(x: Stream[Int]): Stream[Int] = if (x.isEmpty) Stream() else {
val pivot = x.head
val (a, b) = x.tail.partition(_ <= pivot)
quicksort(a) #::: pivot #:: quicksort(b)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment