I. Divide and conquer
Divide and conquer (divide and CONQUER,DNC) is a problem-solving approach, its core is the use of recursive functions, and constantly turn a problem into a smaller problem, until the solution to the problems of the idea of solving.
Ii. examples of divide and conquer problem solving
1, question: If you are a farmer, you have a piece of 1680x680 land, now requires you to divide the land into uniform blocks, and the size of the block as large as possible, now find the largest square side length.
Divide and conquer: Divide and conquer the process of solving the problem consists of two steps: ① Find the simplest case (basic condition); ② constantly reduce the scale of the problem.
Analysis: Since the original land is a rectangle, the simplest case is that the rectangle can be divided into two squares with a short edge as the side length. But obviously the current rectangle does not meet this condition. So we can separate two side length 640 square and a 640x400 rectangular land, then we find a way to reduce the size of the problem: each time with a short edge for the side length of the square, the rest of the rectangle is divided by the same method, until a rectangle appears twice times the width of its length.
Euclidean algorithm:
2, the sum of the list [2, 4, 6, 8, 10] can not be used for the loop, can not be used in the sums function, the algorithm could be applied in any sequence.
The first step is to find the simplest case: The simplest case is that the list has only 1 numbers, so the sum of the list is equal to this number;
The second step is to find a way to reduce the size of the problem (recursive function): Write a recursive function that adds the first number of the list to the rest of the number each time.
code example:
Three, quick sort
Fast sequencing is a sort of method of using the DNC idea, which is much faster than the choice of sorting.
The first step is to find the basic condition: when there are only 1 numbers in the list; the second step is to write a recursive function that reduces the size of the problem: each time the first element of the list is selected as the reference value, and then the number less than the reference value is placed in the sub-list to the left of the reference value, Then, you run the quick sort on the sub-list.
code example:
Note that there is a trap here, that is if Len (alist) < 2 This if statement cannot be written as if Len (alist) = = 1, because if pivot is the largest or smallest number in the list, greater or less is an empty list.
Iv. Speed of fast sequencing
Each layer calls the stack, the operand is 1, and the height of the fast ordered call stack (operand) The worst case is n, and the best case is log2n.
In each layer of the call stack, each layer of the element to be manipulated is one less, so the average time of the n layer is N/2.
So the worst case for a quick sort is O (n) XO (N/2) =o (n2), the constant part has little effect on the result, and the best case is O (log2n) XO (N/2) =o (n log2n), which also goes to the constant part. But here, the best case is the average, so the average run time for fast sequencing is O (n logn).
Legend Explanation:
1. The worst scenario for the call stack:
2, Call stack best case:
3, each layer call stack operand: (because the last constant part will be shed, so here each layer of the operand can be simple to think is N)
Specific reference: "Algorithm Diagram" chapter fourth-Quick sorting
—————— the end of this article!
6. Quick Sort