summaryrefslogtreecommitdiff
path: root/test/benchmark/sources/sort1/sort1.scala
blob: 6795158a7809921038cee1156a12e338b13222f3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/*                     __                                               *\
**     ________ ___   / /  ___     Scala benchmark suite                **
**    / __/ __// _ | / /  / _ |    (c) 2003-2004, LAMP/EPFL             **
**  __\ \/ /__/ __ |/ /__/ __ |                                         **
** /____/\___/_/ |_/____/_/ | |                                         **
**                          |/                                          **
** $Id$
\*                                                                      */


package benchmarks;

/** Quick sort with a functional taste. */
object sort1 with scala.testing.Benchmark {

  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 run: Unit = sort(List.range(1, 10000));
}