summaryrefslogtreecommitdiff
path: root/test/files/run/Course-2002-04.scala
diff options
context:
space:
mode:
authorpaltherr <paltherr@epfl.ch>2003-07-18 13:51:23 +0000
committerpaltherr <paltherr@epfl.ch>2003-07-18 13:51:23 +0000
commite9c280e68e136274fea9d88b40af6645e0fbb970 (patch)
treefb96b7dfbbb21a98877db0a4380cf8cb63a61c96 /test/files/run/Course-2002-04.scala
parent30309b2ba2d8bfa6fb656cd760f6315581204d1d (diff)
downloadscala-e9c280e68e136274fea9d88b40af6645e0fbb970.tar.gz
scala-e9c280e68e136274fea9d88b40af6645e0fbb970.tar.bz2
scala-e9c280e68e136274fea9d88b40af6645e0fbb970.zip
- Merged sort1 into Course-2002-04
Diffstat (limited to 'test/files/run/Course-2002-04.scala')
-rw-r--r--test/files/run/Course-2002-04.scala85
1 files changed, 84 insertions, 1 deletions
diff --git a/test/files/run/Course-2002-04.scala b/test/files/run/Course-2002-04.scala
index 7370b9150c..714996e435 100644
--- a/test/files/run/Course-2002-04.scala
+++ b/test/files/run/Course-2002-04.scala
@@ -43,6 +43,88 @@ object M0 {
object M1 {
+ def mergesort[a] (less : (a,a) => Boolean) (xs: Array[a]): Unit = {
+
+ def While(def c: Boolean)(def b: Unit): Unit =
+ if (c) { b ; While(c)(b) } else ();
+
+ def swap(i: Int, j: Int): Unit = {
+ val t = xs(i);
+ val u = xs(j);
+ xs(i) = u;
+ xs(j) = t;
+ }
+
+ def sort1(l: Int, r: Int): Unit = {
+ val pivot = xs((l + r) / 2);
+ var i = l, j = r;
+ While (i <= j) {
+ While (less(xs(i), pivot)) { i = i + 1 }
+ While (less(pivot, xs(j))) { 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 (xs.length > 0) sort1(0, xs.length - 1);
+ }
+
+ def list2array(list: List[Int]): Array[Int] = {
+ val array = new Array[Int](list.length);
+ list.copyToArray(array, 0);
+ array;
+ }
+
+ def array2list(array: Array[Int]): List[Int] = {
+ var list = List[Int]();
+ List.range(0, array.length).map(i => list = array(i) :: list);
+ list.reverse;
+ }
+
+ def isort(list: List[Int]): List[Int] = {
+ val array = list2array(list);
+ mergesort[Int]((x,y) => x < y)(array);
+ array2list(array);
+ }
+
+ def test = {
+ val list0 = List();
+ val list1 = List(0);
+ val list2 = List(0,1);
+ val list3 = List(1,0);
+ val list4 = List(0,1,2);
+ val list5 = List(1,0,2);
+ val list6 = List(0,1,2);
+ val list7 = List(1,0,2);
+ val list8 = List(2,0,1);
+ val list9 = List(2,1,0);
+ val listA = List(6,3,1,8,7,1,2,5,8,4);
+
+ System.out.println("list0: " + list0 + " -> " + isort(list0));
+ System.out.println("list1: " + list1 + " -> " + isort(list1));
+ System.out.println("list2: " + list2 + " -> " + isort(list2));
+ System.out.println("list3: " + list3 + " -> " + isort(list3));
+ System.out.println("list4: " + list4 + " -> " + isort(list4));
+ System.out.println("list5: " + list5 + " -> " + isort(list5));
+ System.out.println("list6: " + list6 + " -> " + isort(list6));
+ System.out.println("list7: " + list7 + " -> " + isort(list7));
+ System.out.println("list8: " + list8 + " -> " + isort(list8));
+ System.out.println("list9: " + list9 + " -> " + isort(list9));
+ System.out.println("listA: " + listA + " -> " + isort(listA));
+ System.out.println();
+ }
+
+}
+
+//############################################################################
+
+object M2 {
+
def horner (x : Double, coefs : List[Double]) : Double = {
if (coefs.isEmpty)
0
@@ -63,7 +145,7 @@ object M1 {
//############################################################################
-object M2 {
+object M3 {
def dotproduct (v : List[Double], w : List[Double]) : Double = {
if (v.isEmpty)
@@ -154,6 +236,7 @@ object Test {
M0.test;
M1.test;
M2.test;
+ M3.test;
()
}
}