Boolean) (xs: List[T]): List[T] = { def mer-6ren">
gpt4 book ai didi

scala - 来自 "Programming Scala"的合并排序导致堆栈溢出

转载 作者:行者123 更新时间:2023-12-04 01:34:14 25 4
gpt4 key购买 nike

以下算法的直接剪切和粘贴:

def msort[T](less: (T, T) => Boolean)
(xs: List[T]): List[T] = {
def merge(xs: List[T], ys: List[T]): List[T] =
(xs, ys) match {
case (Nil, _) => ys
case (_, Nil) => xs
case (x :: xs1, y :: ys1) =>
if (less(x, y)) x :: merge(xs1, ys)
else y :: merge(xs, ys1)
}
val n = xs.length / 2
if (n == 0) xs
else {
val (ys, zs) = xs splitAt n
merge(msort(less)(ys), msort(less)(zs))
}
}

在 5000 个长列表上导致 StackOverflowError。

有没有什么办法可以优化这个,这样就不会发生?

最佳答案

这样做是因为它不是尾递归的。您可以通过使用非严格集合或使其成为尾递归来解决此问题。

后一个解决方案是这样的:

def msort[T](less: (T, T) => Boolean) 
(xs: List[T]): List[T] = {
def merge(xs: List[T], ys: List[T], acc: List[T]): List[T] =
(xs, ys) match {
case (Nil, _) => ys.reverse ::: acc
case (_, Nil) => xs.reverse ::: acc
case (x :: xs1, y :: ys1) =>
if (less(x, y)) merge(xs1, ys, x :: acc)
else merge(xs, ys1, y :: acc)
}
val n = xs.length / 2
if (n == 0) xs
else {
val (ys, zs) = xs splitAt n
merge(msort(less)(ys), msort(less)(zs), Nil).reverse
}
}

使用非严格性涉及按名称传递参数,或使用非严格性集合,例如 Stream .以下代码使用 Stream只是为了防止堆栈溢出,还有 List别处:
def msort[T](less: (T, T) => Boolean) 
(xs: List[T]): List[T] = {
def merge(left: List[T], right: List[T]): Stream[T] = (left, right) match {
case (x :: xs, y :: ys) if less(x, y) => Stream.cons(x, merge(xs, right))
case (x :: xs, y :: ys) => Stream.cons(y, merge(left, ys))
case _ => if (left.isEmpty) right.toStream else left.toStream
}
val n = xs.length / 2
if (n == 0) xs
else {
val (ys, zs) = xs splitAt n
merge(msort(less)(ys), msort(less)(zs)).toList
}
}

关于scala - 来自 "Programming Scala"的合并排序导致堆栈溢出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2201472/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com