进行双重选择排序方法,即使没有问题,我的代码也无法通过测试

时间:2018-12-08 05:02:20

标签: java sorting double selection error-checking

这是我的数据结构和算法类的最终项目的一部分;我已经一遍又一遍地对其进行了测试,对代码进行了无数次测试,甚至达到了使用骰子模拟随机生成的数字并手动执行程序的目的。我完全不知所措!似乎没有任何测试可以告诉我发生了什么事情,除了代码 在程序中四处移动。在这一点上,我觉得我需要另一双更有经验的眼睛。任何帮助都将受到赞赏。

class ViewController: UIViewController, ARSCNViewDelegate {

    @IBOutlet var sceneView: ARSCNView!

    var isTorchOn = false

    private func turnTorch(enabled: Bool) {
        guard let device = AVCaptureDevice.default(for: AVMediaType.video) else {
            return // Cant initiate avcapturedevice error
        }

        if device.hasTorch {
            do {
                try device.lockForConfiguration()

                if enabled {
                    device.torchMode = .on
                } else {
                    device.torchMode = .off
                }
                self.isTorchOn = enabled // *** Add this line! ***

                device.unlockForConfiguration()
            } catch {
                return
            }

        } else {
            return
        }
    }

    func renderer(_ renderer: SCNSceneRenderer, didRenderScene scene: SCNScene, atTime time: TimeInterval) {
        if self.sceneView.session.currentFrame != nil {
            if !isTorchOn {
                turnTorch(enabled: true)
            }
        }
    }

    func sessionWasInterrupted(_ session: ARSession) {
        // Probably you would want to turn the torch off here.
    }

    func sessionInterruptionEnded(_ session: ARSession) {
        // Probably you would want to turn the torch on again here.
    }

}

1 个答案:

答案 0 :(得分:0)

我发现了问题。在分别在HighBound或LowBound中找到最小值或最大值的情况下,当程序将其认为是最大值或最小值的内容移到错误的区域时,这些值会混合在一起。修复后的代码如下。

public void sort(T[] input) {
    System.out.println("~~~~~~~Begin Sort~~~~~~~"); //debug line
    System.out.println("Initial array as follows:"); //debug line
    for (int i = 0; i < input.length; ++i){
            System.out.print(input[i] + ", ");
        } //debug line
        System.out.print("\n\n"); //debug line
    int min; //Holds index of smallest item in array
    int max; //Holds index of largest item in array
    int lowBound = 0; //Holds index of the smallest index being explored;
                      //initialized as 0, the lowest possible index value
    int highBound = input.length - 1; //Holds index of the largest index
                                      //being explored; initialized as
                                      //highest index value in input[]
    T trader; //Holds value being traded

    while ((highBound - lowBound) > 0) {
        min = lowBound;  //Both of these are initialized and replaced with
        max = highBound; //the new highest and lowest values each time the
                         //while loop repeats
        System.out.println("\nMin and max set at " + min + " and " + max +
                " respectively"); //debug line
        System.out.println("Exploring region bound by " + lowBound + " and " + highBound);
        for (int i = lowBound; i <= highBound; i++) {
            if (input[max].compareTo(input[i]) < 0) {
                max = i;
                System.out.println("new max found at " + i); //debug line
            } //Sets max to i (the new value) if the data at i is found to
              //be greater than the data at max
            if (input[min].compareTo(input[i]) > 0) {
                min = i;
                System.out.println("new min found at " + i); //debug line
            } //Sets min to i (the new value) if the data at i is found to
              //be less than the data at max
        } //Explores the region set by the bounds declared earlier
        if (min == highBound && max == lowBound) {
            trader = input[min]; //Sets the trader to val @ lowBound
            System.out.println("Trader is " + trader); //debug line
            input[min] = input[max]; //Sets val @ lowBound to val @ min
            input[max] = trader; //Sets val @ min to val in the trader 
            System.out.println("lowBound is now " + input[lowBound] +
                    " and highBound is now " + input[highBound]); //debug line
        } //Ensures min and max are not each other to prevent double-
          //swapping (allowing for two different swaps as the need arises)
        else {
            if (min != lowBound){
                trader = input[lowBound]; //Sets the trader to val @ lowBound
                System.out.println("Trader is " + trader); //debug line
                input[lowBound] = input[min]; //Sets val @ lowBound to val @ min
                input[min] = trader; //Sets val @ min to val in the trader 
                System.out.println("lowBound is now " + input[lowBound] +
                        " and min is now " + input[min]); //debug line
                if (max == lowBound) {
                    max = min;
                }//ensures that the program doesn't get mixed up if the
                 //numbers get switched around
            } //Swaps the values at min and lowBound if a change is necessary
            if (max != highBound){
                trader = input[highBound]; //Sets the trader to val @ highBound
                System.out.println("Trader is " + trader); //debug line
                input[highBound] = input[max]; //Sets val @ highBound to val @
                                               //max
                input[max] = trader; //Sets val @ max to val in the trader
                 System.out.println("HighBound is now " + input[highBound] +
                        " and max is now " + input[max]); //debug line
                if (min == highBound) {
                    min = max;
                }//technically unnecessary, but symmetrical code is pretty
            }//Swaps the values at max and highBound if a change is necessary
        } //Swaps the values at highBound and lowBound if an outright switch
          //is necessary

        System.out.print("new array "); //debug line
        for (int i = 0; i < input.length; ++i){
            System.out.print(input[i] + ", ");
        } //debug line
        System.out.print("\n\n"); //debug line
        highBound--;
        lowBound++;
    }
    System.out.println("~~~~~~~~End Sort~~~~~~~~~\n\n"); //debug line
}

}