Experiment 1: Divide and Conquer

  • The content of the algorithm class is only to understand the implementation process of the algorithm, and the specific code implementation and the only knowing process are two different degrees of mastery of algorithm knowledge. Here, the classic algorithm in the algorithm course including the content of the experiment report is carried out. Summarize the finishing and summary of the code implementation.

  • The basic idea of ​​divide and conquer

    What is divide and conquer? As the name suggests, the method of divide and conquer, and more specifically, is to decompose a complex problem into two or more identical or similar sub-problems. These sub-problems can be further divided into smaller sub-problems so that they can be solved directly and simply. , The solution of the original problem is the combination of the solutions of the sub-problems.

  • The design idea of ​​the divide-and-conquer method is to divide a big problem that is difficult to solve directly into some smaller-scale, same problems, so that each can be broken and divided and conquered.

  • Divide: Divide the array into two groups (it is also possible that the number of the two arrays is one), find the maximum value of the two arrays respectively, and then divide the opened array into equal parts, and divide the final result into equal parts. The number of numbers in the array is 1 or 2.
    The number of numbers in the sub-array is 1 ; set the changed numbers to the maximum and minimum values.
    The number of numbers in the word array is 2 : Compare the size of the two numbers, assign the uncle to the maximum value, and the decimal to the minimum value.

  • Backtracking: I feel that this piece of code is difficult to understand when it is implemented, because the backtracking is not synchronized with the recursive call. Wait a minute, in that case, it should be just the opposite of the recursive call! ! ! Backtracking at the end of the first recursive call! ! !
    Suddenly understand the idea of ​​recursive call! ! ! So happy! ! ! I continue! ! !

  • The next step is the implementation of the specific code. Let me write the code by myself first and try to write it out by myself!

  • Recursion is indeed understood, but the function that uses pointers to pass addresses really forgot how to use it. Maybe I was not very proficient at that time! ! How to use it?

  • ok!!! Refer to the standard format code if you have any doubts for a clearer idea.

  • For example, when the code is implemented, the call in the main function needs to be written in the **& parameter format of the address , and when the function is defined, it is written in the form of a pointer such as void MMaxmin (int i, int j, int max, int min), in that case The pointer of the function directly calls the address of the parameter that needs to be passed in the main function, and the parameter * in the definition function can be directly connected with the parameter in the main function.

  • Divide and conquer binary search to find the best value code is as follows:

在这里插入代码片
#include <stdio.h>
int a[9]={
    
    1,2,3,4,5,6,7,8,9};
void MMaxmin(int i,int j,int *max,int *min);
int main(){
    
    
	int fmax,fmin;
	MMaxmin(0,8,&fmax,&fmin);
	printf("MAX=%d,MIN=%d\n",fmax,fmin);
}
void MMaxmin(int i,int j,int *max,int *min){
    
    
	int lmax,lmin,rmax,rmin;
	if(i==j){
    
    
		*max=a[i];*min=a[i];
	}
	else if(i+1==j){
    
    
		if(a[i]<a[j]){
    
    *min=a[i];*max=a[j];}
		else {
    
    *min=a[j];*max=a[i];}
	}
	else {
    
    
		int mid=(i+j)/2;
		MMaxmin(i,mid,&lmax,&lmin);
		MMaxmin(mid+1,j,&rmax,&rmin);
		if(lmin<rmin) *min=lmin;
		else *min=rmin;
		if(lmax>rmax) *max=lmax;
		else *max=rmax;
	}
}

Guess you like

Origin blog.csdn.net/CSDN_Ysu/article/details/109003017