algorithm - 使用归并排序计算反转次数

标签 algorithm scala sorting mergesort

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

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 < ymatch 中的第二行)

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:

实际上它并没有正确计数,我找不到错误所在。

最佳答案

这是我的 Frege solution 的 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)
      }

}

关于algorithm - 使用归并排序计算反转次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17636180/

相关文章:

C编程: Sorting Structures with two parameters

c++ - 试图理解二进制插入排序?

algorithm - 计算实时事件频率的替代方法

java - 如何以最低价格优化购物车?

javascript - Peak and Flag Codility 最新挑战

scala - 为什么 Scala 中有 RichInt 或 RichX?

scala - Scala的 “powerful”类型系统是什么?

mysql - 首先在 mysql 中选择最匹配的行

algorithm - 如何实现伪随机函数

scala - 如何在 case 类中指定多个构造函数?