I am ashamed to say that I have learned the C language for so long. Even recursion and sub-governance are not very useful for so many questions. I still have to make a good summary, or else it will be ruined. I found that learning is really progressive. What I learned before is not very understandable. Now I think it is very simple and not so hard to understand. Maybe the progress has been improved. Maybe it was not enough effort. In any case, I learned it as my own.
For recursion, we generally know the factorial. 1*2*3 *...... * N. You can call it by yourself recursively. The returned return value is continuously reduced, knowing that it is 1. This is generally easy to understand. The problem is caused by other problems, but it is not that simple.
For divide and conquer, major problems are divided into small problems, and then small problems are further divided into smaller problems. After the problems are solved, the problems are merged into the original solution. In fact, sub-governance uses recursion, or uses recursion directly. How important is Recursion to learn algorithms well. To learn recursion, we need to learn the implementation of its recursive algorithm. There are not many codes, but we will solve it quickly with our ideas.
To find the maximum and minimum values, I believe it is no longer easy. As long as a loop traversal is performed, the maximum and minimum values are compared each time. After the traversal, the maximum and minimum values are obtained.
In addition, sort first, and then the maximum and minimum values are the beginning and end.
Now that we have talked about sub-governance and recursion, we need to use the sub-division method to find the maximum and minimum values. If you don't want to talk about it much, you should first go to the code.
# Include <stdio. h> int AA [] = {1, 5, 0,-8, 4, 2}; void get_max_min (int * s, int * Nmax, int * Nmin, int left, int right) {int max1, max2; int min1, min2; int mid; If (Left = right) // The minimum sub-problem is one number, the maximum and minimum values are both {* Nmax = * Nmin = s [left]; return;} else if (Left = (right-1 )) // The minimum sub-problem is two numbers, so the maximum and minimum values are obtained by comparison. {Max1 = s [left]; max2 = s [right]; * Nmax = max1> max2? Max1: max2; * Nmin = max1 <max2? Max1: max2; return;} else // otherwise, the sub-problem is not small enough. Continue to divide {mid = (left + right)/2; get_max_min (S, & max1, & min1, left, mid); get_max_min (S, & max2, & min2, Mid + 1, right); * Nmax = max1> max2? Max1: max2; // calculate the maximum and minimum values for the solution merge of subproblems * Nmin = min1 <min2? Min1: min2; return ;}} int main () {int Max, min; get_max_min (AA, & MAX, & min, 0, 5); printf ("Max: % d \ n ", max); printf (" Min: % d \ n ", min); Return 0 ;}
After reading the code + comments, I believe I can still understand it? If you still don't understand it, it's okay. Let's continue to analyze it step by step.
The figure above shows the data process. Function call and recursion. This is the simplest method of grouping and recursion.