From e9c280e68e136274fea9d88b40af6645e0fbb970 Mon Sep 17 00:00:00 2001 From: paltherr Date: Fri, 18 Jul 2003 13:51:23 +0000 Subject: - Merged sort1 into Course-2002-04 --- test/files/run/Course-2002-04.scala | 85 ++++++++++++++++++++++++++++++++++++- 1 file changed, 84 insertions(+), 1 deletion(-) (limited to 'test/files/run/Course-2002-04.scala') 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; () } } -- cgit v1.2.3