If you have scala installed, you can copy and paste the code to a file, compile it (scalac filename), run (scala compiledfilename) and see the output. Enjoy!
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 QuickSort { | |
def sort(xs: Array[Int]): Array[Int] = { | |
if (xs.length <= 1) xs | |
else { | |
val pivot = xs(xs.length / 2) | |
val lessThanPivot = xs filter (pivot >) | |
val equalToPivot = xs filter (pivot ==) | |
val greaterThanPivot = xs filter (pivot <) | |
println("Pivot for array [%s] is %d".format(xs.deep.mkString(" "), | |
pivot)) | |
println("[%s] [%s] [%s]\n".format(lessThanPivot.deep.mkString(" "), | |
equalToPivot.deep.mkString(" "), | |
greaterThanPivot.deep.mkString(" "))) | |
Array.concat(sort(lessThanPivot), equalToPivot, sort(greaterThanPivot)) | |
} | |
} | |
def main(args: Array[String]) { | |
val inputArray = Array(10,9,8,3,7,6,5,4,3,2,1) | |
println("Input Array : [%s]\n".format(inputArray.deep.mkString(" "))) | |
println("Sorted Array: [%s]".format(sort(inputArray).deep.mkString(" "))) | |
} | |
} |