1

そして、2 つの値を比較するための比較関数 "compr" が既にコードに含まれています。

私はこのようなものが欲しい:

Sorting.stableSort(arr[i,j] , compr)

ここで、arr[i,j] は配列内の要素の範囲です。

4

4 に答える 4

0

これは、並べ替えを再実装せずに得られるのと同じくらい良いはずです。並べ替えられるスライスのサイズを持つ追加の配列を 1 つだけ作成します。

def stableSort[K:reflect.ClassTag](xs:Array[K], from:Int, to:Int, comp:(K,K) => Boolean) : Unit = {
  val tmp = xs.slice(from,to)
  scala.util.Sorting.stableSort(tmp, comp)
  tmp.copyToArray(xs, from)
}
于 2013-07-10T23:20:53.040 に答える
0

そんな感じ:

def stableSort[T](x: Seq[T], i: Int, j: Int, comp: (T,T) => Boolean ):Seq[T] = {
    x.take(i) ++ x.slice(i,j).sortWith(comp) ++ x.drop(i+j-1)
}                                         

def comp: (Int,Int) => Boolean = { case (x1,x2) => x1 < x2 } 
val x = Array(1,9,5,6,3)                           
stableSort(x,1,4, comp)
// > res0: Seq[Int] = ArrayBuffer(1, 5, 6, 9, 3)

クラスが Ordering を実装していれば、それほど面倒ではありません。

于 2013-07-10T12:23:22.830 に答える