diff options
author | michelou <michelou@epfl.ch> | 2003-11-28 17:04:18 +0000 |
---|---|---|
committer | michelou <michelou@epfl.ch> | 2003-11-28 17:04:18 +0000 |
commit | daea8b76a5f9dd35e54f4d524691187850202182 (patch) | |
tree | 69409f806b178c804446def99b96f7a017d3cdf9 /sources/examples | |
parent | 6b8d116ec9f10e97fd2fb3baf7ebd99af0ebedf2 (diff) | |
download | scala-daea8b76a5f9dd35e54f4d524691187850202182.tar.gz scala-daea8b76a5f9dd35e54f4d524691187850202182.tar.bz2 scala-daea8b76a5f9dd35e54f4d524691187850202182.zip |
- updated to current Scala compiler
- added main() for testing
Diffstat (limited to 'sources/examples')
-rw-r--r-- | sources/examples/sort.scala | 53 | ||||
-rw-r--r-- | sources/examples/sort1.scala | 22 | ||||
-rw-r--r-- | sources/examples/sort2.scala | 31 |
3 files changed, 70 insertions, 36 deletions
diff --git a/sources/examples/sort.scala b/sources/examples/sort.scala index 36d91b72ae..661ae0a414 100644 --- a/sources/examples/sort.scala +++ b/sources/examples/sort.scala @@ -1,27 +1,42 @@ -module sorter { +object sorter { -def sort(a: Array[Int]): Unit = { + def sort(a: Array[Int]): Unit = { - def swap(i: Int, j: Int): Unit = { - val t = a(i); a(i) = a(j); a(j) = t; - } + def swap(i: Int, j: Int): Unit = { + val t = a(i); a(i) = a(j); a(j) = t; + } - def sort1(l: Int, r: Int): Unit = { - val pivot = a((l + r) / 2); - var i = l, j = r; - while (i <= j) { - while (a(i) < pivot) { i = i + 1 } - while (a(j) > pivot) { j = j - 1 } - if (i <= j) { - swap(i, j); - i = i + 1; - j = j - 1; + def sort1(l: Int, r: Int): Unit = { + val pivot = a((l + r) / 2); + var i = l, j = r; + while (i <= j) { + while (a(i) < pivot) { i = i + 1 } + while (a(j) > pivot) { j = j - 1 } + if (i <= j) { + swap(i, j); + i = i + 1; + j = j - 1; + } } + if (l < j) sort1(l, j); + if (j < r) sort1(i, r); } - if (l < j) sort1(l, j); - if (j < r) sort1(i, r); + + sort1(0, a.length - 1); + } + + def println(ar: Array[Int]) = { + def iter(i: Int): String = + ar(i) + (if (i < ar.length-1) "," + iter(i+1) else "]"); + Console.println("[" + iter(0)); + } + + def main(args: Array[String]) = { + val xs = List(6, 2, 8, 5, 1); + val ar = xs.copyToArray(new Array[Int](xs.length), 0); + println(ar); + sort(ar); + println(ar); } - sort1(0, a.length - 1); -} }
\ No newline at end of file diff --git a/sources/examples/sort1.scala b/sources/examples/sort1.scala index f2722b23cc..13a7b3e568 100644 --- a/sources/examples/sort1.scala +++ b/sources/examples/sort1.scala @@ -1,12 +1,20 @@ -import scala._; - -module sorter { +object sorter { def sort(a: List[Int]): List[Int] = { - val pivot = a at (a.length / 2); - sort(a.filter(x => x < pivot)) - ::: a.filter(x => x == pivot) - ::: sort(a.filter(x => x > pivot)) + if (a.length < 2) + a + else { + val pivot = a(a.length / 2); + sort(a.filter(x => x < pivot)) + ::: a.filter(x => x == pivot) + ::: sort(a.filter(x => x > pivot)) + } + } + + def main(args: Array[String]) = { + val xs = List(6, 2, 8, 5, 1); + Console.println(xs); + Console.println(sort(xs)); } }
\ No newline at end of file diff --git a/sources/examples/sort2.scala b/sources/examples/sort2.scala index a4e32a9cd4..f143958471 100644 --- a/sources/examples/sort2.scala +++ b/sources/examples/sort2.scala @@ -1,12 +1,23 @@ -module sorter { +object sorter { + + def sort(a: List[Int]): List[Int] = { + if (a.length < 2) + a + else { + val pivot = a(a.length / 2); + def lePivot(x: Int) = x < pivot; + def gtPivot(x: Int) = x > pivot; + def eqPivot(x: Int) = x == pivot; + sort(a filter lePivot) + ::: sort(a filter eqPivot) + ::: sort(a filter gtPivot) + } + } + + def main(args: Array[String]) = { + val xs = List(6, 2, 8, 5, 1); + Console.println(xs); + Console.println(sort(xs)); + } -def sort (a: List[Int]): List[Int] = { - val pivot = a at (a.length / 2); - def leqPivot(x: Int) = x <= pivot; - def gtPivot(x: Int) = x > pivot; - def eqPivot(x: Int) = x == pivot; - sort(a filter leqPivot) - ::: sort(a filter eqPivot) - ::: sort(a filter gtPivot) -} }
\ No newline at end of file |