Skip to content

Instantly share code, notes, and snippets.

@zetashift
Created December 7, 2018 02:53
Show Gist options
  • Select an option

  • Save zetashift/ffd95724bbc73372a0459e2374a399eb to your computer and use it in GitHub Desktop.

Select an option

Save zetashift/ffd95724bbc73372a0459e2374a399eb to your computer and use it in GitHub Desktop.
QuickSort in Scala
object QS {
def quickSort(xs: Array[Int]): Array[Int] = {
if (xs.length < 2) xs
else {
val pivot = xs(xs.length/2)
val less = xs.filter(n => n < pivot)
val greater = xs.filter(n => n > pivot)
quickSort(less) ++ Array(pivot) ++ quickSort(greater)
}
}
}
val r = scala.util.Random
val randomArray = (for (i <- 1 to 1000) yield r.nextInt(1000)).toArray
println(QS.quickSort(randomArray).mkString)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment