【发布时间】:2017-04-02 13:05:47
【问题描述】:
我无法找到时间复杂度。
- 首先,谈到 MergeSort 中的外部 FOR,我认为重复是 (1+ Sumation(from i=1, to sizeOfArray)(2*i) = 1+(2+4+8+16+32 +...+size) 但我也认为我错了。
- 我在测量内部 FOR 循环重复时也遇到了问题。
MergeSort(){ //迭代版本(自下而上)
for(int currentSize = 1; currentSize < length; currentSize *= 2) {
for(int low = 0; low < length - currentSize; low += 2*currentSize){
int mid = low + currentSize - 1;
//min() is used here so if low is very close to the end of the array, high doesn't take outOfBoundries Value.
int high = Math.min(low + currentSize*2 -1, length - 1);
}
}
}
合并(int low,int middle,int high){
// Copy both parts into the helper array
for (int i = low; i <= high; i++) {
helper[i] = arrayForMergeSort[i];
}
int i = low;
int j = middle + 1;
int k = low;
// Copy the smallest values from either the left or the right side back
// to the original array
while (i <= middle && j <= high) {
if (helper[i] <= helper[j]) {
arrayForMergeSort[k] = helper[i];
i++;
} else {
arrayForMergeSort[k] = helper[j];
j++;
}
k++;
}
// Copy the rest of the left side of the array into the target array
while (i <= middle) {
arrayForMergeSort[k] = helper[i];
k++;
i++;
}
}
【问题讨论】:
标签: sorting time-complexity mergesort