2

我需要使用合并排序来计算反转次数:

object Example {
 def msort(xs: List[Int]): List[Int] = {
    def merge(left: List[Int], right: List[Int]): Stream[Int] = (left, right) match {
      case (x :: xs, y :: ys) if 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(ys), msort(zs)).toList
    }
  }                                              

  msort(List(8, 15, 3))                           
}

我想我必须把它算在行中(其中y < y,第二行match

case (x :: xs, y :: ys) => Stream.cons(y, merge(left, ys))

但是,当我尝试时,我失败了。

我怎么做?

更新

带有累加器的版本:

def msort(xs: List[Int]): List[Int] = {
    def merge(left: List[Int], right: List[Int], inversionAcc: Int = 0): Stream[Int] = (left, right) match {
      case (x :: xs, y :: ys) if x < y => Stream.cons(x, merge(xs, right, inversionAcc))
      case (x :: xs, y :: ys) => Stream.cons(y, merge(left, ys, inversionAcc + 1))
      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(ys), msort(zs)).toList
    }
  } 

我如何轻松返回inversionAcc?我想,我只能像这样将它作为元组的一部分返回:

def merge(left: List[Int], right: List[Int], invariantAcc: Int = 0): (Stream[Int], Int)

不过,看起来不太好。

更新2

它实际上并没有正确计算,我找不到错误在哪里。

4

2 回答 2

2

这是我的Frege 解决方案的 Scala 端口。

object CountInversions {

  def inversionCount(xs: List[Int], size: Int): (Int, List[Int]) = 
    xs match {
        case _::_::_ => { //If the list has more than one element
          val mid = size / 2
          val lsize = mid
          val rsize = size - mid
          val (left, right) = xs.splitAt(mid)
          val (lcount, lsorted) = inversionCount(left, lsize)
          val (rcount, rsorted) = inversionCount(right, rsize)
          val (mergecount, sorted) = inversionMergeCount(lsorted, lsize, rsorted,
            rsize, 0, Nil)
          val count = lcount + rcount + mergecount
          (count, sorted)
        }
        case xs => (0, xs)
     }

  def inversionMergeCount(xs: List[Int], m: Int, ys: List[Int], n: Int, 
    acc: Int, sorted: List[Int]): (Int, List[Int]) = 
      (xs, ys) match {
        case (xs, Nil) => (acc, sorted.reverse ++ xs)
        case (Nil, ys) => (acc, sorted.reverse ++ ys)
        case (x :: restx, y :: resty) => 
          if (x < y) inversionMergeCount(restx, m - 1, ys, n, acc, x :: sorted)
          else if (x > y) inversionMergeCount(xs, m, resty, n - 1, acc + m, y :: sorted)
          else inversionMergeCount(restx, m - 1, resty, n - 1, acc, x :: y :: sorted)
      }

}
于 2013-07-14T05:28:27.280 回答
1

如果解决方案不必严格执行功能,那么您只需添加一个简单的计数器:

object Example {
  var inversions = 0
  def msort(xs: List[Int]): List[Int] = {
    def merge(left: List[Int], right: List[Int]): Stream[Int] = (left, right) match {
      case (x :: xs, y :: ys) if x < y => Stream.cons(x, merge(xs, right))
      case (x :: xs, y :: ys) =>
        inversions = inversions + 1
        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(ys), msort(zs)).toList
    }
  }
}
Example.msort(List(8, 15, 3))
println(Example.inversions)

如果它必须保持功能,那么您需要创建一个累加器并通过所有方法调用将其线程化,并从每个函数返回一个 Pair,其中累加器值包含在返回结果中,然后对每个合并的累加器值求和收敛。(我的function-fu不是很好,在尝试简单的var方法之前,我已经尝试过在功能上解决这个问题。)

于 2013-07-14T04:46:11.940 回答