Created
May 28, 2014 15:09
-
-
Save tiggreen/28861555ee32a02a314a to your computer and use it in GitHub Desktop.
QuickSort in Scala
This file contains 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 Sorting { | |
def main(args: Array[String]) { | |
var lst = Array(1, 6, 2, 12, 22, 8, 99, 45, 12, 28, 1, 54) | |
val sortedList = quickSort(lst) | |
for(i <- sortedList) { | |
println(i) | |
} | |
} | |
def quickSort(lst: Array[Int]): Array[Int] = { | |
if (lst.length <= 1) lst | |
else { | |
val pivot = lst(lst.length / 2) | |
Array.concat( | |
quickSort(lst filter (pivot >)), | |
lst filter (pivot ==), | |
quickSort(lst filter (pivot <))) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
val
instead ofvar
!