I've tried to write a parallel Mergesort using Scala Futures. However, when I run my algorithm on a list of size 100 000 inside Eclipse's interpreter everything gets very sluggish and eventually I get an error message telling me I'm out of memory. When I run it in the interpreter from the command line it hangs already at lists of size 10 000 (but now I get no error messages).
Why does this happen and is there a fix?
import scala.actors.Future
import scala.actors.Futures._
object MergeSort{
def sort[T <% Ordered[T]](toBeSorted :List[T]) :List[T] = toBeSorted match{
case Nil => Nil
case List(x) => List(x)
case someList =>
val (left, right) = someList splitAt someList.length/2
val sortedLeft = future { sort(left) }
val sortedRight = sort(right)
merge(sortedLeft(), sortedRight, Nil)
}
def merge[T <% Ordered[T]](a :List[T], b :List[T], Ack: List[T]) :List[T] = (a, b) match {
case (Nil, ys) => Ack.reverse ++ ys
case (xs, Nil) => Ack.reverse ++ xs
case (x::xs, y::ys) if x < y => merge(xs, y::ys, x::Ack)
case (x::xs, y::ys) => merge(x::xs, ys, y::Ack)
}
}