Created
September 2, 2011 18:22
-
-
Save cgopalan/1189389 to your computer and use it in GitHub Desktop.
Scala Quicksort
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(" "))) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment