Advertisement
spkenny

scala qsort

Feb 11th, 2015
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.41 KB | None | 0 0
  1. def qsort[T <% Ordered[T]](list : List[T] ) : List[T] =
  2.   {
  3.     if ( list.isEmpty )
  4.       list
  5.     else if ( list.size == 1 )
  6.       List[T](list(0))
  7.     else
  8.     {
  9.       val first = list(0)
  10.       val tail = list.tail
  11.       val left = tail.filter( { (x : T) =>  x < first }  )
  12.       val right = tail.filter( { (x : T) =>  x >= first } )
  13.       qsort(left) ++ List[T](first) ++ qsort(right)
  14.     }
  15.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement