Switching to function quick sort version

This commit is contained in:
Dan Buch 2012-05-14 00:35:39 -04:00
parent 537036f41b
commit 10bf8b31e5

View File

@ -1,23 +1,12 @@
object sort { object sort {
def sort(xs: Array[Int]) { def sort(xs: Array[Int]): Array[Int] = {
def swap(i: Int, j: Int) { if (xs.length <= 1) xs
val t = xs(i); xs(i) = xs(j); xs(j) = t else {
} val pivot = xs(xs.length / 2)
def sort1(l: Int, r: Int) { Array.concat(
val pivot = xs((l + r) / 2) sort(xs filter (pivot >)),
var i = l; var j = r xs filter (pivot ==),
while (i <= j) { sort(xs filter (pivot <)))
while (xs(i) < pivot) i += 1
while (xs(j) > pivot) j -= 1
if (i <= j) {
swap(i, j)
i += 1
j -= 1
} }
} }
if (l < j) sort1(l, j)
if (j < r) sort1(i, r)
}
sort1(0, xs.length - 1)
}
} }