我是计算机科学专业的学生学习算法和时间复杂度。
我正在尝试计算使用冒泡排序而不是插入排序的 shell 排序的最佳、最差和平均情况的时间复杂度。这就是我写的那种。
public static <T extends Comparable<T>>
void shellSort(T[] data)
{
int length = data.length;
int gap = length/2;
boolean swapped;
//gap is reduced in half each pass until the gap is zero
while(gap > 0){
swapped = true;
//will keep iterating until we get not values swapped
while(swapped){
swapped = false;
for(int scan = 0; scan < (length - gap); scan++){
//swaps larger values to the right accross a gap
if(data[scan].compareTo(data[scan + gap]) > 0){
swap(data, scan, (scan + gap));
swapped = true;
printArr(data);
}
}
}
gap = gap/2;
}
}
我发现最好的情况是我们有一个排序数组,这将产生 O(n*logn) 时间复杂度。
在我看来,最坏的情况是我们会有 O(n^2*logn) 的复杂度?
我试图了解中间循环将如何改变整体复杂性。