首先是插入排序:
static void insertionSort(int[] array) {
for (int i = 1; i < array.length; i++) {
int key = array[i];
int j = i - 1;
while (j >= 0 && array[j] > key) {
array[j + 1] = array[j];
j--;
}
array[j + 1] = key;
}
}
时间复杂度主要取决于以下几行,因为这是完成比较和交换操作的地方。
while (j >= 0 && array[j] > key) {
array[j + 1] = array[j];
j--;
swapCount++;
}
拿一张纸,为每个 j 值绘制一个交换表。
最终,你会明白算法进入 while 循环 (n-k) 次,每当进入时,它都会进行交换操作 k 次。所以,时间复杂度是(n-k)*k。
让我们证明一下。
将交换计数器变量放入算法中。
static int insertionSort(int[] array) {
int swapCount = 0;
for (int i = 1; i < array.length; i++) {
int key = array[i];
int j = i - 1;
while (j >= 0 && array[j] > key) {
array[j + 1] = array[j];
j--;
swapCount++;
}
array[j + 1] = key;
}
return swapCount;
}
现在,让我们在问题中描述的数组上尝试一下。
public class App {
public static void main(String[] args) throws Exception {
int[] baseArray = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int n = baseArray.length;
int k = 3;
// Shift base array by k
int[] shiftedArray = shiftArray(baseArray, k);
// Calculate how many swaps done by the insertion sort
int swapCount = InsertionSort.insertionSort(shiftedArray);
// Theroitical value is calculated by using the formula (n-k)*k
int timeComplexityTheoritical = (n - k) * k;
System.out.print("Theoritical Time Complexity based on formula: " + timeComplexityTheoritical);
System.out.print(" - Swap Count: " + swapCount);
System.out.print(" - Is formula correct:" + (timeComplexityTheoritical == swapCount) + "
");
}
// Shift array to the right circularly by k positions
static int[] shiftArray(int[] array, int k) {
int[] resultArray = array.clone();
int temp, previous;
for (int i = 0; i < k; i++) {
previous = resultArray[array.length - 1];
for (int j = 0; j < resultArray.length; j++) {
temp = resultArray[j];
resultArray[j] = previous;
previous = temp;
}
}
return resultArray;
}
static class InsertionSort {
static int insertionSort(int[] array) {
int swapCount = 0;
for (int i = 1; i < array.length; i++) {
int key = array[i];
int j = i - 1;
while (j >= 0 && array[j] > key) {
array[j + 1] = array[j];
j--;
swapCount++;
}
array[j + 1] = key;
}
return swapCount;
}
}
}
输出:
基于公式的理论时间复杂度:21 - 交换计数:21 - Is
公式正确:正确
我试过大小为 2^16 的数组并将其移动 2^16-1 次,每次公式都是正确的。