Divide and conquer the problem of finding the maximum value, typical error demonstration and correct code

    The author found a more interesting mistake when studying the divide and conquer method and discussing it with my classmates, and I would like to share it with you here.

    We know that the core idea of ​​the divide and conquer method is to divide and conquer the objects to be processed first , and when the objects to be processed are basically ordered, the divide and conquer method will degenerate into a bubbling algorithm.

    Let's look at an interesting error example (pseudocode):   

void MaxMin(A[l..r],Max,Min)
{
	if(r==l)
	{
		if(A[1]>Max) Max=A[l];
		else if(A[1]<Min) Min=[l];
	}
	else{    	
	   	MaxMin(A[l,(l+r)/2],Max1,Min1); //Recursively solve the previous part
		MaxMin(A[(l+r/)2+1..r],Max2,Min2); //Recursively solve the latter part			
	}

}

    The above program can indeed find the most value, but it is worth noting that although it separates the array to be processed, it is not a divide and conquer, in other words, it does not use the divide and conquer algorithm at all, but bubble sort . We understand the structure separated by the divide-and-conquer method as a binary tree. Its processing idea is to compare the maximum value with the leaf nodes. No matter how the objects to be sorted change, its time complexity is exactly the same as that of bubble sort ( In fact, this is the recursive formula of bubble sort). Next, we use the structure of the binary tree to understand the idea of ​​divide and conquer through the combination of graphics and text:

    D, E, F, and G are obtained after dividing, and the idea of ​​solving the most value is that each node is the optimal solution of the tree with it as the root node. For example, the most value obtained by B is generated after DE comparison, and C The maximum value obtained is generated after the FG comparison, and the value obtained by A is generated after the BC comparison. At this point, if there are still readers who are not clear, I will make a simple analogy: In the above binary tree, if the bubble sort is used to find the maximum value, the MAX is compared by DEFG in turn (the number of comparisons is four), and the divide and conquer method will DE is compared to B, FG is compared to C, and BC is compared to A (the number of comparisons is three).

Next, I will post a pseudo-code for the correct divide and conquer method to find the most value for your reference:

void MaxMin(A[l..r],Max,Min)
{
	if(r==l)
	{//When there is only one element
		Max=A[l];
		Min = [l];
	}
	else
	{
	   if(r-l==1) //When there are two elements
	   {
	   	if (A[l]<=A[r]){
		  	Max=A[r];
		   	Min = A [l];
	   	}
	     	else{
		 	Max=A[l];  
			Min = A [r];
	     	}
	   }
	   else{//r-l>1   	
	   	MaxMin(A[l,(l+r)/2],Max1,Min1); //Recursively solve the previous part
		MaxMin(A[(l+r/)2+1..r],Max2,Min2); //Recursively solve the latter part
	   }
		if (Max1<Max2)  
			Max= Max2;//Select the largest value from the two maximum values ​​of the two parts
		if (Min2<Min1)  
			Min=Min2; // select the smallest value from the two minimum values ​​of the two parts
	}

}
    This is the author's discussion on the divide and conquer method from the perspective of seeking the most value. The gains are inevitably not objective enough. Readers are welcome to leave a message for discussion.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326045811&siteId=291194637