2. Algorithm research of divide and conquer-search for the largest contiguous subset in the array and 2014-3-11 11:37 read (16)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.