我有两种不同类型的两种实现,InsertionSort 和 ShellSort。
它们如下:
插入排序:
for (int pos = 0; pos < arrayToBeSorted.length; pos++) {
for (int secondMarker = pos; secondMarker > 0; secondMarker--) {
int currentValue = arrayToBeSorted[secondMarker];
int valueBeingCheckedAgainst = arrayToBeSorted[secondMarker - 1];
if (currentValue > valueBeingCheckedAgainst) {
break;
}
arrayToBeSorted[secondMarker] = arrayToBeSorted[secondMarker - 1];
arrayToBeSorted[secondMarker - 1] = currentValue;
}
}
贝壳排序:
for (int gap = a.length / a.length; gap > 0; gap = (gap / 2)) {
for (int i = gap; i < a.length; i++) {
int tmp = a[i];
int j = i;
for (; j >= gap && tmp < (a[j - gap]); j -= gap) {
a[j] = a[j - gap];
}
a[j] = tmp;
}
}
我还有 10 个包含 32000 个整数的整数数组。我得到了在这些类中调用静态 sortArray 方法之前的时间。结果如下:
对于 InsertionSort.sortArray:
Solving array with: 32000 elements.
Time in milliseconds:264
Time in milliseconds:271
Time in milliseconds:268
Time in milliseconds:263
Time in milliseconds:259
Time in milliseconds:257
Time in milliseconds:258
Time in milliseconds:260
Time in milliseconds:259
Time in milliseconds:261
对于 ShellSort:
Solving array with: 32000 elements.
Time in milliseconds:357
Time in milliseconds:337
Time in milliseconds:167
Time in milliseconds:168
Time in milliseconds:165
Time in milliseconds:168
Time in milliseconds:167
Time in milliseconds:167
Time in milliseconds:166
Time in milliseconds:167
那么他们之间怎么会有这么大的区别呢?它们基本上是相同的算法?
此外,ShellSort 的前 2 次运行怎么可能需要更长的时间,但其余的运行速度更快?
这是 128000 个元素的结果,再次先进行 InsertionSort:
Solving array with: 128000 elements.
Time in milliseconds:4292
Time in milliseconds:4267
Time in milliseconds:4241
Time in milliseconds:4252
Time in milliseconds:4253
Time in milliseconds:4248
Time in milliseconds:4261
Time in milliseconds:4260
Time in milliseconds:4333
Time in milliseconds:4261
贝壳排序:
Solving array with: 128000 elements.
Time in milliseconds:5358
Time in milliseconds:5335
Time in milliseconds:2676
Time in milliseconds:2656
Time in milliseconds:2662
Time in milliseconds:2654
Time in milliseconds:2661
Time in milliseconds:2656
Time in milliseconds:2660
Time in milliseconds:2673
我确信我传递给方法的数组是完全相同的,而且它们是非常随机的。