Advertisement
Guest User

Untitled

a guest
May 30th, 2016
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.71 KB | None | 0 0
  1.  
  2.   def mergeSort(xs: List[(File, Long)]): List[(File, Long)] = {
  3.  
  4.     val m = xs.length / 2
  5.  
  6.     if (m == 0) xs
  7.     else {
  8.  
  9.       @tailrec
  10.       def merge(xs: List[(File, Long)], ys: List[(File, Long)], accu: List[(File, Long)]): List[(File, Long)] =
  11.  
  12.         (xs, ys) match {
  13.  
  14.           case (Nil, Nil) => accu.reverse
  15.           case (Nil, y :: ys1) => merge(xs, ys1, y :: accu)
  16.           case (x :: xs1, Nil) => merge(xs1, ys, x :: accu)
  17.           case (x :: xs1, y :: ys1) =>
  18.  
  19.             if (x._2 < y._2) merge(xs1, ys, x :: accu)
  20.             else merge(xs, ys1, y :: accu)
  21.  
  22.         }
  23.  
  24.       val (left, right) = xs splitAt m
  25.  
  26.       merge(mergeSort(left), mergeSort(right), List())
  27.  
  28.     }
  29.  
  30.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement