Introduction to Algorithms

Source: Internet
Author: User

The introduction to algorithms explains the second typical example of the divide and conquer algorithm. Find the largest sub-array in an array. That is, find the continuous addition and the largest part in this array!
First, we can analyze the problem and find that if we divide the array into two parts, we can find the maximum sub-array of each part and the maximum sub-array spanning the two parts, that is, the complete solution to this problem.

We found that the most complex part is the largest sub-array that spans two parts. let's solve this part first.

First, let's look at the Code:


[Cpp]


[Cpp]
<SPAN style = "FONT-SIZE: 18px"> int Find_MAX_CROSSING_SUBARRAY (int * A, int low, int mid, int high, int & max_left, int & max_right, int & max_value)
{
Int left_sum =-1000000; // the minimum value that is not possible
Int sum = 0;

For (int I = mid; I> low; I --)
{
Sum = sum + A [I];
If (sum> left_sum)
{
Left_sum = sum;
Max_left = I;
}
}

Int right_sum =-1000000; // the minimum value that is not possible
Sum = 0;

For (int j = mid + 1; j {
Sum = sum + A [j];
If (sum> right_sum)
{
Right_sum = sum;
Max_right = j;
}
}

Max_value = left_sum + right_sum;
Return 0;
} </SPAN>

Int Find_MAX_CROSSING_SUBARRAY (int * A, int low, int mid, int high, int & max_left, int & max_right, int & max_value)
{
Int left_sum =-1000000; // the minimum value that is not possible
Int sum = 0;
 
For (int I = mid; I> low; I --)
{
Sum = sum + A [I];
If (sum> left_sum)
{
Left_sum = sum;
Max_left = I;
}
}
 
Int right_sum =-1000000; // the minimum value that is not possible
Sum = 0;
 
For (int j = mid + 1; j {
Sum = sum + A [j];
If (sum> right_sum)
{
Right_sum = sum;
Max_right = j;
}
}
 
Max_value = left_sum + right_sum;
Return 0;
}


We can see from the algorithm that we still put two impossible sentinel values. Each line in the code above is clear, so we will not analyze it here. If you do not understand it, you can leave a message. Then let's write the recursion part of the function. The condition for exit of recursion is the key. Imagine how to find the maximum subarray if low and high are the same value, simply return this value, so the algorithm is done in this way, and then paste the Code:


[Cpp]
Int Find_MaxiMum_SubArray (int * A, int low, int high, int & max_left, int & max_right, int & max_value)
{
If (high = low)
{
Max_left = low;
Max_right = high;

Max_value = A [low];
}
Else
{
Int mid = (low + high)/2;

Int tmp_left_low;
Int tmp_left_high;
Int tmp_left_sum;

Find_MaxiMum_SubArray (A, low, mid, tmp_left_low, tmp_left_high, tmp_left_sum );

Int tmp_right_low;
Int tmp_right_high;
Int tmp_right_sum;

Find_MaxiMum_SubArray (A, mid + 1, high, tmp_right_low, tmp_right_high, tmp_right_sum );

Int tmp_cross_low;
Int tmp_cross_high;
Int tmp_cross_sum;

Find_MAX_CROSSING_SUBARRAY (A, low, mid, high, tmp_cross_low, tmp_cross_high, tmp_cross_sum );

If (tmp_left_sum> = tmp_right_sum) & (tmp_left_sum> = tmp_cross_sum ))
{
Max_left = tmp_left_low;
Max_right = tmp_left_high;
Max_value = tmp_left_sum;
}
Else if (tmp_right_sum> = tmp_left_sum) & (tmp_right_sum> = tmp_cross_sum ))
{
Max_left = tmp_right_low;
Max_right = tmp_right_high;
Max_value = tmp_right_sum;
}
Else
{
Max_left = tmp_cross_low;
Max_right = tmp_cross_high;
Max_value = tmp_cross_sum;
}

}

Return 0;
}

Int Find_MaxiMum_SubArray (int * A, int low, int high, int & max_left, int & max_right, int & max_value)
{
If (high = low)
{
Max_left = low;
Max_right = high;

Max_value = A [low];
}
Else
{
Int mid = (low + high)/2;

Int tmp_left_low;
Int tmp_left_high;
Int tmp_left_sum;

Find_MaxiMum_SubArray (A, low, mid, tmp_left_low, tmp_left_high, tmp_left_sum );

Int tmp_right_low;
Int tmp_right_high;
Int tmp_right_sum;

Find_MaxiMum_SubArray (A, mid + 1, high, tmp_right_low, tmp_right_high, tmp_right_sum );

Int tmp_cross_low;
Int tmp_cross_high;
Int tmp_cross_sum;

Find_MAX_CROSSING_SUBARRAY (A, low, mid, high, tmp_cross_low, tmp_cross_high, tmp_cross_sum );

If (tmp_left_sum> = tmp_right_sum) & (tmp_left_sum> = tmp_cross_sum ))
{
Max_left = tmp_left_low;
Max_right = tmp_left_high;
Max_value = tmp_left_sum;
}
Else if (tmp_right_sum> = tmp_left_sum) & (tmp_right_sum> = tmp_cross_sum ))
{
Max_left = tmp_right_low;
Max_right = tmp_right_high;
Max_value = tmp_right_sum;
}
Else
{
Max_left = tmp_cross_low;
Max_right = tmp_cross_high;
Max_value = tmp_cross_sum;
}

}
 
Return 0;
}
Then I wrote a call code:


[Cpp]
Int B [10] = {1,-10, 2,-15, 6,-8 };
 
Int main (int argc, char ** argv)
{
 
Cout <endl;

Int max_left, max_right, max_value;
Find_MaxiMum_SubArray (B, 0, 10, max_left, max_right, max_value );
Cout <max_left <"\ t" <max_right <"\ t" <max_value <endl;
Cout <endl;
System ("pause ");
Return 0;
}

Int B [10] = {1,-10, 2,-15, 6,-8 };

Int main (int argc, char ** argv)
{

Cout <endl;
 
Int max_left, max_right, max_value;
Find_MaxiMum_SubArray (B, 0, 10, max_left, max_right, max_value );
Cout <max_left <"\ t" <max_right <"\ t" <max_value <endl;
Cout <endl;
System ("pause ");
Return 0;
}
Well, this algorithm is another typical example of the divide and conquer algorithm. This is because it is necessary to perform recursion based on actual conditions ~.

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.