Research on the algorithm of divide and conquer
var cc=console
function Find_max_crossing_subarray (A,low,mid,high) {
var max_left=mid,max_right=mid
var left_sum=0
var sum=0
for (Var i=mid;i>=low;i--) {
Sum=sum+a[i]
if (sum>left_sum) {
Left_sum=sum
Max_left=i
}
}
var right_sum=0
var sum=0
for (Var i=mid+1;i<=high;i++) {
Sum=sum+a[i]
if (sum>right_sum) {
Right_sum=sum
Max_right=i
}
}
return [Max_left,max_right,left_sum+right_sum]
}
Search for the largest contiguous subset across the midpoint and
var arr=[13,-3,-2,20,-3,-16,-23,18,20,-7,12,-5,-22,15,-4,7]
var Re=find_max_crossing_subarray (arr,0,8,arr.length-1)
Cc.log (RE)//=>18,20,-7,12function Find_maximum_subarray (A,low,high) {
if (High==low) {
return [Low,high,a[low]]
}else{
var mid= (Low+high) >>1/*[left_low,left_high,left_sum]*/
var Left=find_maximum_subarray (A,low,mid)
/*[right_low,right_high,right_sum]*/
var Right=find_maximum_subarray (A,mid+1,high)
/*[cross_low,cross_high,cross_sum]*/
var Cross=find_max_crossing_subarray (A,low,mid,high) if (left[2]>=right[2]&&left[2]>=cross[2]) {
return left
}else if (right[2]>=left[2]&&right[2]>=cross[2]) {
return right
}else if (cross[2]>=left[2]&&cross[2]>=right[2]) {
Return Cross
}
}
}
Searches for the largest contiguous subset in the array and
var Re2=find_maximum_subarray (arr,0,arr.length-1)
Cc.log (Re2)//=>18,20,-7,12 [7,10,42]
2. Algorithm research of divide and conquer-search for the largest contiguous subset in the array and 2014-3-11 11:37 read (16)