我正在尝试分析执行合并和插入排序的运行时间。我注意到一个奇怪的趋势,我无法通过谷歌搜索找到任何有相同问题的人,但我可能使用了错误的术语。
排序算法运行的次数似乎与算法完成所花费的时间量成反比。下面显示了插入排序的示例。
4213,2104,8195,9441,4823,925,980,964,911,491,470,482,481 ...(结算时间约490毫秒)
与合并排序类似的行为,但合并在~95ms结算。
我不知道为什么会发生这种情况,我每次都会生成一个随机数组......即使它不是随机的,也不应该每次都(或接近)完全相同的时间?为什么它会收敛到一个较小的值?
只是寻找可能知道为什么会发生这种情况的人,因为我觉得这对我来说真的很奇怪......我认为这可能是Java在幕后做的事情吗?任何建议/提示赞赏!
我正在运行的所有代码都发布在下面。首先显示测试代码。
public static void tests(int noTests, int arraySize){
//set up the running totals of the time taken by insertion and merge
double insertSum = 0;
double mergeSum = 0;
for(int i = 0; i < noTests; i++){
//generate an array of random integers
Integer[] randInput = generateRandomArray(arraySize);
Integer[] insertInput = Arrays.copyOf(randInput, randInput.length);
Integer[] mergeInput = Arrays.copyOf(randInput, randInput.length);
//start the clock for insertion
final long insertionStart = System.nanoTime();
//sort it
insertionSort(insertInput);
//stop the clock for insertion
final long insertionFinish = System.nanoTime();
System.out.println("Time taken for insertion: " + (insertionFinish - insertionStart)/1000 + " ms");
//add it to the running total
insertSum += (insertionFinish - insertionStart)/1000;
//likewise for merge
final long mergeStart = System.nanoTime();
mergeSort(mergeInput);
final long mergeFinish = System.nanoTime();
System.out.println("Time taken for merge: " + (mergeFinish - mergeStart)/1000 + " ms");
mergeSum += (mergeFinish - mergeStart)/1000;
}
//Get the average by diving by the number of times it ran
System.out.println("-------------------------------------------------------");
System.out.println("Insert average: " + insertSum/noTests);
System.out.println("Merge average: " + mergeSum/noTests);
}
//Generate an array of random Integers
public static Integer[] generateRandomArray(int n){
Integer[] arr = new Integer[n];
for(int i = 0; i < n; i++){
arr[i] = (int) Math.floor(Math.random()*100);
}
return arr;
}
public static <T extends Comparable<T>> T[] insertionSort(T[] a){
for(int i = 1; i < a.length; i++){
int j = i-1;
T key = a[i];
while(j >= 0 && a[j].compareTo(key) > 0){
a[j+1] = a[j];
j = j-1;
}
a[j+1] = key;
}
return a;
}
@SuppressWarnings("rawtypes")
public static Comparable[] mergeSort(Comparable[] input){
if(input.length<=1){
return input;
}
int middle = Math.floorDiv(input.length, 2);
Comparable a[] = new Comparable[middle];
for(int i = 0; i < middle; i++){
a[i] = input[i];
}
Comparable b[] = new Comparable[input.length - middle];
for(int i = middle; i < input.length; i++){
b[i-middle] = input[i];
}
mergeSort(a);
mergeSort(b);
merge(input, a, b);
return input;
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public static void merge(Comparable[] input, Comparable[] a, Comparable[] b){
int inputIndex = 0;
int aIndex = 0;
int bIndex = 0;
while(aIndex < a.length && bIndex < b.length){
if(aIndex < a.length & a[aIndex].compareTo(b[bIndex]) < 0){
input[inputIndex] = a[aIndex];
aIndex++;
} else{
input[inputIndex] = b[bIndex];
bIndex++;
}
inputIndex++;
}
}
示例输出:
Time taken for insertion: 8060 ms
Time taken for merge: 1714 ms
Time taken for insertion: 11533 ms
Time taken for merge: 23418 ms
Time taken for insertion: 5674 ms
Time taken for merge: 326 ms
Time taken for insertion: 8235 ms
Time taken for merge: 459 ms
Time taken for insertion: 9737 ms
Time taken for merge: 333 ms
Time taken for insertion: 4756 ms
Time taken for merge: 374 ms
Time taken for insertion: 1088 ms
Time taken for merge: 493 ms
Time taken for insertion: 899 ms
Time taken for merge: 1147 ms
Time taken for insertion: 783 ms
Time taken for merge: 474 ms
Time taken for insertion: 653 ms
Time taken for merge: 252 ms
-------------------------------------------------------
Insert average: 5141.8
Merge average: 2899.0
谢谢!
编辑:更新了引用错误,插入和合并现在都在对自己的数组进行排序。问题依然存在。更新的示例输出,如果给定更多术语,则插入最终仍会收敛于比启动时低得多的值
答案 0 :(得分:2)
您将randInput
传递给插入排序,然后将其传递给合并排序。
在Java中,数组通过引用传递。在call by reference
中,如果在方法中更改其数组,则更改的数组将可用于调用数组。
所以randInput
在传递给mergesot
方法时会被排序。
见:
//generate an array of random integers
Integer[] randInput = generateRandomArray(arraySize);
// randInput is random
insertionSort(randInput);
// randInput is sorted
mergeSort(randInput);