Swift:以合并排序计算掉期

Swift:以合并排序计算掉期,swift,mergesort,Swift,Mergesort,我正在尝试以合并排序计算掉期。这似乎是一个非常直截了当的命题,但我的逻辑似乎有问题 下面是我试图增加计数的代码的相关部分: while leftIndex < leftPile.count && rightIndex < rightPile.count { if leftPile[leftIndex] < rightPile[rightIndex] { // nothing swapped here

我正在尝试以合并排序计算掉期。这似乎是一个非常直截了当的命题,但我的逻辑似乎有问题

下面是我试图增加计数的代码的相关部分:

    while leftIndex < leftPile.count && rightIndex < rightPile.count {
        if leftPile[leftIndex] < rightPile[rightIndex] {
            // nothing swapped here
            orderedPile.append(leftPile[leftIndex])
            leftIndex += 1
        } else if leftPile[leftIndex] > rightPile[rightIndex] {
            orderedPile.append(rightPile[rightIndex])
            // item taken from right pile == swapped -> increment swapCount
            swapCount += 1
            rightIndex += 1
        } else {
            orderedPile.append(leftPile[leftIndex])
            leftIndex += 1
            // item taken from left pile == swapped -> increment swapCount
            swapCount += 1
            orderedPile.append(rightPile[rightIndex])
            rightIndex += 1
        }
    }
这是我的全班同学:

class MergeSortWithCounter {

    var swapCount: Int64

    init() {
        swapCount = 0
    }

    func mergeSort<T: Comparable>(_ array: [T]) -> [T] {
        guard array.count > 1 else { return array }
        let middleIndex = array.count / 2
        let leftArray = mergeSort(Array(array[0..<middleIndex]))
        let rightArray = mergeSort(Array(array[middleIndex..<array.count]))
        return merge(leftPile: leftArray, rightPile: rightArray)
    }

    func merge<T: Comparable>(leftPile: [T], rightPile: [T]) -> [T] {

        var leftIndex = 0
        var rightIndex = 0
        var orderedPile = [T]()
        if orderedPile.capacity < leftPile.count + rightPile.count {
            orderedPile.reserveCapacity(leftPile.count + rightPile.count)
        }

        while leftIndex < leftPile.count && rightIndex < rightPile.count {
            if leftPile[leftIndex] < rightPile[rightIndex] {
                // nothing swapped here
                orderedPile.append(leftPile[leftIndex])
                leftIndex += 1
            } else if leftPile[leftIndex] > rightPile[rightIndex] {
                orderedPile.append(rightPile[rightIndex])
                // item taken from right pile == swapped -> increment swapCount
                swapCount += 1
                rightIndex += 1
            } else {
                orderedPile.append(leftPile[leftIndex])
                leftIndex += 1
                // item taken from left pile == swapped -> increment swapCount
                swapCount += 1
                orderedPile.append(rightPile[rightIndex])
                rightIndex += 1
            }
        }

        while leftIndex < leftPile.count {
            orderedPile.append(leftPile[leftIndex])
            leftIndex += 1
        }

        while rightIndex < rightPile.count {
            orderedPile.append(rightPile[rightIndex])
            rightIndex += 1
        }

        return orderedPile
    }
}
class MergeSortWithCounter{
var swapCount:Int64
init(){
swapCount=0
}
func mergeSort(u数组:[T])->[T]{
guard array.count>1 else{return array}
让middleIndex=array.count/2
让leftArray=mergeSort(数组[0..rightPile[rightIndex]{
追加(rightPile[rightIndex])
//从右堆中取出的项目==交换->增量交换计数
swapCount+=1
rightIndex+=1
}否则{
追加(leftPile[leftIndex])
leftIndex+=1
//从左堆取走的项目==交换->增量交换计数
swapCount+=1
追加(rightPile[rightIndex])
rightIndex+=1
}
}
而leftIndex
我知道我错过了一些非常简单的事情,但我花了比我愿意承认的更多的时间来找出我的错误所在


感谢您的阅读!

您在不必进行以下操作的情况下,以同等的方式进行交换:

        if leftPile[leftIndex] < rightPile[rightIndex] {
            // nothing swapped here
如果leftPile[leftIndex]
你是说

        if leftPile[leftIndex] <= rightPile[rightIndex] {
            // nothing swapped here

如果leftPile[leftIndex]为什么认为5是错误的?(抱歉,如果我太过密集,但我对正确答案没有期望。所以我真的是在问,而不是批评。)另外,你还有一个很棒的调试器,所以你当然可以一步一步地检查代码,看看发生了什么。谢谢你的阅读。这是一个黑客等级测试数组。他们说它是4。我想我的问题在于else语句。我现在不在我的机器附近,但当我在它前面时,我会修补它。@Pierce我有一个问题对于更大的数据集来说,这是一个挑战。我太便宜了,买不了一堆测试用例,但其中一位评论员说他用了很长时间来解决一个容量问题,使用了一个普通的
Int
。这就是为什么我有一个
Int64
作为我的计数器。我一路弄明白后会告诉你的。我将把这个放在一个Xcode项目中t、 要弄清楚这不是一个游乐场。在没有断点的情况下,数组等有很多问题需要解决。@Adrian-我发现了我一直超时的一个主要原因,那就是我忘了在
mergeSort
的开头放
guard
语句,所以它一直运行到超时为止,试图对数组进行排序只有一个实体。即使在我修复了一半的测试用例之后,我仍然有问题。让我们看看是否可以修复这个问题。通过尝试使用Swift解决这个特定问题,我已经疯了。另一个我使用Swift解决的严重问题是处理尝试的数据结构问题。
        if leftPile[leftIndex] <= rightPile[rightIndex] {
            // nothing swapped here