排序实现,相同的测试用例

时间:2016-04-05 00:29:56

标签: java algorithm implementation

我喜欢:( X - 不同的算法)

public class XAlgorithm{
 sort(List l){...}
}

在testClass中,它显示如下:

ArrayList array = new ArrayList(...); // original array

public static void main(String[]args){

    AlgorithmsTest at = new AlgorithmsTest();
    at.testInsertSort();
    // when add at.array.printAll() - method printing all elements, there are no changes to original array what I want
    at.testBubbleSort();
    at.testSelectSort();
    at.testShellSort(); 

}



 testBubbleSort{
    ...
    ArrayList arrayBubble = new ArrayList(testBubble.sort(array));
    ...
}

问题是我的结果(System.currentTimeMilis()测量的时间)在我为ex启动时是不同的。它连续两次使用相同的算法,它也很奇怪,因为即使我在每个方法中完成复制(通过将所有新元素放入新数组然后对其进行操作)仍然有效。对于第一个算法,无论是哪个算法,时间总是最大的。

我甚至控制每个算法之间的数组(比如上面代码中的//注释)并且它是正确的 - 没有更改它,所以问题在哪里:/?

提前致谢

1 个答案:

答案 0 :(得分:0)

即使你声明你正在制作阵列的副本,但听起来你正在进行排序,然后制作阵列的副本。

因此,第一次将花费最长时间,但所有后续运行都需要做的工作量较少,因为数组已排序"。

它似乎也说你的排序算法中存在错误,这样你就可以在第一次排序时接近(或者它是正确的),但随后的排序就是找到一个角落的情况,造成轻微的排序数组中的变化。我正在分析我的排序方法,并确保它们按照您的意图正常工作。