quick sort


See On Github

Data

Source Code

/** QuickSort algorithm in Scala.
 *  
 *  Written by Tim Leathart, January 2014
 */

object QuickSort {
  def QuickSort  (is : List[Int]) : List[Int] = is match {
    case Nil => List() 			// Empty list already sorted
    case h :: Nil => List(h)	// List with one item already sorted
    case h :: tail => {		
      val less = QuickSort(tail.filter(x => x < h))	
      val more = QuickSort(tail.filter(x => x >= h))
      less ++ List(h) ++ more
	}
  }
  
}
object QuickSort_Test {

  def main(args: Array[String]): Unit = {
    val list = List(5,2,5,7,8,1,2,4,6,8,2,3)    
    println("Before: " + list.mkString(", "))
    
    val sorted = QuickSort.QuickSort(list);
    println("After: " + sorted.mkString(", "))
  }

}