summaryrefslogtreecommitdiff
path: root/sources/examples/sort2.scala
diff options
context:
space:
mode:
Diffstat (limited to 'sources/examples/sort2.scala')
-rw-r--r--sources/examples/sort2.scala31
1 files changed, 21 insertions, 10 deletions
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