值更改但仅用作参数

时间:2017-03-14 16:13:51

标签: java php arrays algorithm

我必须在PHP中用Java编写我的实现vor insert / selection-sort。因此,算法工作正常,但是在编写快速程序时,为了保存我的多类型值,使用Logger类时,后面的所有不同类型都很麻烦。

由于我还没有使用Java一段时间,代码中可能存在导致这个奇怪错误的问题。

我必须遍历整数数组才能生成和排序不同长度的随机数组:

 public static Log[][] eval(Integer[] sizes, Algorithm alg){

        Log[][] logs = new Log[sizes.length][2];
        for (int i = 0; i < sizes.length; i++) {
            Log[] tmp = new Log[3];
            Integer[] rand = randomArray(sizes[i]);
            System.out.println("Before "+Arrays.toString(rand));
            tmp[0] = new Log(sizes[i], rand);
            tmp[1] = alg.insertionSort(rand);
            tmp[2] = alg.selectionSort(rand);
            System.out.println("After "+Arrays.toString(rand));
            logs[i] = tmp;
        }
        return logs;
    }

如您所见,有两个调试,它们将提供如下内容:

  

之前[2,1,4,5,3]

     

[1,2,3,4,5]之后

     

之前[1,8,9,10,5,4,2,6,7,3]

     

[1,2,3,4,5,6,7,8,9,10]之后

     

之前[11,15,20,16,18,8,4,13,2,19,12,3,10,5,17,14,1,   9,6,7]

     

[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18]之后   19,20]

这很奇怪,因为我没有改变兰特值。即使我在循环外部放置一个名为backup的Int数组并覆盖它并在最后一个日志中打印出来,它仍然是一个排序数组。我试图解决这个问题超过一个小时,我无法找到错误。它必须在索引1/2的算法方法中,因为在= =跳过这些行之后。但我不知道什么是错的,因为我只使用数组作为参数并返回一个Log of Object。

这是一种算法方法:

public Log insertionSort(Integer[] sortieren) {
    double startTime = System.nanoTime();
    int temp;
    for (int i = 1; i < sortieren.length; i++) {
        temp = sortieren[i];
        int j = i;
        while (j > 0 && sortieren[j - 1] > temp) {
            sortieren[j] = sortieren[j - 1];
            j--;
        }
        sortieren[j] = temp;
    }
    double stopTime = System.nanoTime();
    double time = stopTime - startTime;
    Log log = new Log("Insertion-Sort", (time/1000000), sortieren);
    return log;
}

这是我的日志类:

public class Log {

    public String name;
    public double time;
    public int size;
    public Integer[] sorted;
    public Integer[] randArray;

    public Log(String name, double time, Integer[] sorted){

        this.name = name;
        this.time = time;
        this.sorted = sorted;
    }

    public Log(int size, Integer[] random){
        this.size = size;
        this.randArray = random;
    }
}

后来,我想通过这种方法评估这个:

 public static void reportLogger(Log[][] log, boolean showArray){

        for (int i = 0; i < log.length; i++) {
          // Show Initial Array (0th Index) -> (size, array)
            for (int j = 1; j <= 2 ; j++) {
                Log tmp = log[i][j];
                // 1st/2nd Index -> Insert/Selection ->  name, time, sortedArray by using tmp.name ,etc..
            System.out.println("---------------------");*/
        }
    }

1 个答案:

答案 0 :(得分:2)

数组是一个输入参数,但它也在第一个排序方法alg.insertionSort(rand)中进行了修改。此时,数组变成了一个排序数组 - 不再是随机数组。在alg.selectionSort(rand)点,他们所做的就是对排序后的数组rand进行排序。为了比较性能,您应该复制数组rand

tmp[0] = new Log(sizes[i], rand);
Integer[] array_copy_1 = Array.copyof(rand, rand.size)
tmp[1] = alg.insertionSort(array_copy_1);
Integer[] array_copy_2 = Array.copyof(rand, rand.size)
tmp[2] = alg.selectionSort(array_copy_2);