Switching to function quick sort version
This commit is contained in:
parent
537036f41b
commit
10bf8b31e5
@ -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)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user