diff options
Diffstat (limited to 'sources/examples/sort1.scala')
-rw-r--r-- | sources/examples/sort1.scala | 22 |
1 files changed, 15 insertions, 7 deletions
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 |