4.1练习
4.1-1
返回的是数组中最大元素的下标和值
4.1-2
public static int[] violenceFindMaximumSubarray(int[] arr) {
int sum = Integer.MIN_VALUE; //this variable is made for the max sum of outer loop
int tempSum = 0; //this variable is made for temporary sum after each iteration
int leftIndex = 0;
int rightIndex = 0;
for(int i = 0;i < arr.length;i++) {
tempSum = 0;
for(int j = i;j < arr.length;j++) {
tempSum += arr[j];
if(tempSum > sum) {
sum = tempSum;
leftIndex = i;
rightIndex = j;
}
}
}
return new int[] {leftIndex,rightIndex,sum};
}
4.1-3
递归算法实现
https://blog.csdn.net/sscout/article/details/80984832
暴力算法实现在4.1-2已经实现
当n0<30时采用暴力算法,其他情况采用分治算法
修改:只需要在分治算法开始时再加上一个判断 数组的长度是否小于30,如果小于30,就调用暴力算法
不会改变性能交叉点
4.1-4
public static int[] findMaxCrossingSubarray(int[] arr,int low,int mid,int high) {
int leftSum = 0;
int rightSum = 0;
int sum = 0;
int maxLeft = mid;
int maxRight = mid;
for(int i = mid;i >= low;i--) {
sum += arr[i];
if(sum > leftSum) {
leftSum = sum;
maxLeft = i;
}
}
sum = 0;
for(int j = mid+1;j <= high;j++) {
sum += arr[j];
if(sum > rightSum) {
rightSum = sum;
maxRight = j;
}
}
if(leftSum+rightSum < 0) {
return new int[] {-1,-1,0};
}
return new int[] {maxLeft,maxRight,leftSum+rightSum};
}
public static int[] findMaximumSubarray(int[] arr,int low,int high) {
if(high == low) {
if(arr[0] < 0) {
return new int[] {-1,-1,0};
}
return new int[] {low,high,arr[0]};
}else {
int mid = (low + high)/2;
int[] leftResult = findMaximumSubarray(arr,low,mid);
int[] rightResult = findMaximumSubarray(arr,mid+1,high);
int[] crossResult = findMaxCrossingSubarray(arr,low,mid,high);
if(leftResult[2] >= rightResult[2] && leftResult[2] >= crossResult[2]) {
return leftResult;
}else if(rightResult[2] >= leftResult[2] && rightResult[2] >= crossResult[2]) {
return rightResult;
}else{
return crossResult;
}
}
}
4.1-5
public static int[] findMaximumSubarrayByMaximumSubarray(int[] arr,int low,int high) {
int subSum = 0;
int sum = 0;
int leftIndex = high + 1;
for(int i = low;i <= high;i++) {
subSum += arr[i];
}
if(subSum + arr[high+1] < subSum) {
return new int[] {low,high,subSum};
}else {
for(int j = high+1;j >= low;j--) {
sum += arr[j];
if(sum > subSum) {
subSum = sum;
leftIndex = j;
}
}
}
return new int[] {leftIndex,high+1,subSum};
}
4.2练习
4.2-1
(1)分解为8个长度为1的数组
A11={1},A12={3}, B11={6},B12={8},
A21={7},A22={5}, B21={4},B22={2},
(2)计算7个p
P1=6
P2=8
P3=72
P4=-10
P5=48
P6=-12
P7=-84
(3)计算C
C11=18
C12=14
C21=62
C22=66
(4)C={{18,14},{62,66}}
4.2-2
https://blog.csdn.net/sscout/article/details/81016217
4.2-3
https://blog.csdn.net/sscout/article/details/81022864
4.2-4
所以k最大是21
T(n)= Θ(n^log3(21))
4.2-5
所以最佳运行时间是70*70的
比Strassen算法的性能好
4.2-6
(kn×n)(n×nk)矩阵相乘得到 (kn×kn)矩阵
∵其中共进行了 k²次(n×n)(n×n)矩阵相乘
∴ 所以共调用 k²次Strassen算法
∴ 耗费了Θ(k²lg7)时间
同理(n×kn)(nk×n)矩阵相乘得到 (n×n) 矩阵,调用了k次Strassen算法
∴ 耗费了Θ(klg7)时间
4.2-7
第一次乘法 (a+b)(c+d) = ac + bd + ad + bc
第二次乘法 bd
第三次乘法 ac
ac - bd
(a+b)(c+d) - bd -ac = ad + bc
4.3练习
4.3-1
4.3-2
4.3-3
4.3-4
4.3-5
4.3-6
4.3-7
4.3-8
4.3-9
4.4练习
4.4-1
4.4-2
4.4-3
4.4-4
4.4-5
4.4-6
4.4-7
4.4-8
4.4-9
4.5练习
4.5-1
a.
b.
c.
d.