Created
December 7, 2018 02:53
-
-
Save zetashift/ffd95724bbc73372a0459e2374a399eb to your computer and use it in GitHub Desktop.
QuickSort in Scala
This file contains hidden or 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
| 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