First quicksort version

although the tutorial did not have a class or object definition,
which is necessary according to sbt ...   hmmm.
This commit is contained in:
Dan Buch
2012-05-14 00:32:10 -04:00
parent 3075bdb938
commit 537036f41b
5 changed files with 40 additions and 0 deletions

View File

@@ -0,0 +1,3 @@
name := "byx"
version := "0.1.0"

View File

@@ -0,0 +1,23 @@
object sort {
def sort(xs: Array[Int]) {
def swap(i: Int, j: Int) {
val t = xs(i); xs(i) = xs(j); xs(j) = t
}
def sort1(l: Int, r: Int) {
val pivot = xs((l + r) / 2)
var i = l; var j = r
while (i <= j) {
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)
}
}