summaryrefslogtreecommitdiff
path: root/sources/examples/sort1.scala
blob: 13a7b3e5682905483d95bf0e0854cf95d35e90ff (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
object sorter {

  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));
  }

}