summaryrefslogblamecommitdiff
path: root/docs/examples/sort2.scala
blob: 1e471b89df312df40370b220401907e73c79a63e (plain) (tree)
1
2
3
4
5
6
7
8
                
 
              




                                       






                                      


     


                                         
                             
   
 
 
package examples

object sort2 {

  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]): Unit = {
    val xs = List(6, 2, 8, 5, 1)
    Console.println(xs)
    Console.println(sort(xs))
  }

}