diff options
Diffstat (limited to 'docs/examples/sort1.scala')
-rw-r--r-- | docs/examples/sort1.scala | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/docs/examples/sort1.scala b/docs/examples/sort1.scala new file mode 100644 index 0000000000..406bf7c911 --- /dev/null +++ b/docs/examples/sort1.scala @@ -0,0 +1,22 @@ +package examples; + +object sort1 { + + def sort(a: List[Int]): List[Int] = { + 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)) + } + +} |