4.1笔记
找出数组中的最大子数组采用分治方法实现
用Java代码实现
public static int[] findMaxCrossingSubarray(int[] arr,int low,int mid,int high){ // low是最小的数组索引下标,high是最大的数组索引下标
int sum = 0;
int leftSum = 0;
int maxLeftIndex = 0;
for (int i = mid;i >= low;i--){
sum += arr[i];
if(sum > leftSum){
leftSum = sum;
maxLeftIndex = i;
}
}
int rightSum = 0;
sum = 0;
int maxRightIndex = 0;
for(int j = mid +1;j <= high;j++){
sum += arr[j];
if(sum > rightSum){
rightSum = sum;
maxRightIndex = j;
}
}
return new int[] {maxLeftIndex,maxRightIndex,leftSum+rightSum};
}
public static int[] findMaximumSubarray(int[] arr,int low,int high){
if(high == low){ //判断该数组是否只有一个元素
return new int[]{low,high,arr[low]};
}else{
int mid = (low + high)/2;
int[] leftMaxArr = findMaximumSubarray(Arrays.copyOfRange(arr,0,mid+1),0,mid);
int[] rightMaxArr = findMaximumSubarray(Arrays.copyOfRange(arr,mid+1,high+1),0,arr.length-mid-2);
int[] crossMaxArr = findMaxCrossingSubarray(arr,low,mid,high);
if(leftMaxArr[2] >= rightMaxArr[2] && leftMaxArr[2] >= crossMaxArr[2]){
return leftMaxArr;
}else if(rightMaxArr[2] >= leftMaxArr[2] && rightMaxArr[2] >= crossMaxArr[2]){
return rightMaxArr;
}else{
return crossMaxArr;
}
}
}
4.2笔记
https://blog.csdn.net/sscout/article/details/81016217
4.3笔记
代入法
4.4笔记
分析递归树实现
4.5笔记
T(n) = a T(n/b) + f(n) ( a ≥ 1 , b ≥ 1 ,n > 0 且是整数 )
1. 如果存在常数 ε > 0 ,使得 f(n) = O(n^㏒b (a-ε)) ,那么 T(n)=Θ (n^㏒b(a)).
2. 如果 f(n)=Θ (n^㏒b(a)) ,那么T(n)= Θ (n^㏒b(a)*lgn).
3. 如果存在常数 ε > 0 ,使得 f(n)=Ω (n^㏒b (a+ε)),并且如果存在常数 c <1 , 使得所有充分大的n都满足 af(n/b)≤ cf(n) , 那么 T(n)= Θ(f(n)).
4.6笔记