Bohtvaroh

Insertion sort in Scala

Apr 27th, 2012
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.33 KB | None | 0 0
  1. object InsertionSort {
  2.   def sort[T <% Ordered[T]](xs: List[T]): List[T] = xs match {
  3.     case Nil => Nil
  4.     case h :: t => insert(h, sort(t))
  5.   }
  6.  
  7.   private def insert[T <% Ordered[T]](x: T, xs: List[T]): List[T] = xs match {
  8.     case Nil => x :: Nil
  9.     case h :: t if x <= h => x :: xs
  10.     case h :: t => h :: insert(x, t)
  11.   }
  12. }
Advertisement
Add Comment
Please, Sign In to add comment