1. The divide-and-conquer model has three steps at each level of recursion : Decomposition, resolution, merger
- 2. Merge sort algorithm fully follows the divide-and-conquer mode :
- Decomposition: Decomposition of the sequence of n elements to be sorted into two sub-sequences with N/2 elements
- Workaround: Sort two sub-sequences recursively using merge sort
- Merge: Merge two sorted sub-sequences to produce sorted answers
- 3. Calculate the time required to analyze the divide-and-conquer algorithm:
- Suppose that T (N) is the run time of an issue with a scale of N, and if the problem is small enough, such as a constant c,n≦c, then the direct solution requires a constant, and we write it θ (1). Assuming that the original problem is decomposed into a sub-problem, the scale of each sub-problem is the original problem 1/b However, we will see that in many divide-and-conquer algorithms a≠b) in order to solve a sub-problem with a scale of n/b, the time of T (n/b) is needed, so the time N of at (n/b) is required to solve a sub problem, if the decomposition problem becomes sub-problem takes time D (n), the solution of the Jia Cheng original problem of (n) then get the recursive formula:
- T (n) ={θ (1) If, n≦c
- at (n/b) +d (n) +c (n) Other
- 4. Merge sort algorithm
the worst-case run-time t (N) recursion is established to set the number of merge sort n, and an element of the merge sort requires constant time when there are n>1
our elemental decomposition run time is as follows:
decomposition: The decomposition step only computes the middle position of the sub-array, which requires constant time, so D (n) =θ (1)
Solution: We recursively solve two sub-problems of both scale N/2, which will contribute 2T (N/2) Run time
Merging: We have noticed that the process merge requires θ (n) time on a word group with n elements , so C (n) =θ (n) when we add the function d (n) and C (n) to the analysis of the merge sort, we are in it. One
theta (n) function is added to another θ (1) function, and the sum is a linear function of N, that is , Θ (n), which is added to 2T (N/2) from the "Resolution" step. , recursion for the worst-case run time t (n) of the merge sort will be given Θ (1) if N=1 T (n) ={2T (N/2) +Θ (n) if n>1 in the 4th chapter, we will see the "Main theorem", which can be used to prove that T (N) is θ (NLGN), where LGN stands for ㏒2n, because the logarithmic function grows slower than any linear function, so the input large enough to run in the worst case is actually θ (NLGN) The merge sort will be better than the insertion sort of the run time Θ (n2) rewrite merge sort algorithm:
c if N=1
T (n) ={2T (N/2) +CN if n>1 ( constant C represents the time required to solve a problem of size 1 and the time it takes to process each array element in the decomposition and merge steps )
LGN + 1 rep layer, CN for each tier contribution total cost
 CNLGN + CN=CN ( LGN + 1) ignoring low order and constant c gives the desired result Θ (NLGN) , &NB Sp , &NB Sp , &NB Sp
Basis of algorithm: Divide and conquer mode, merge sort θθθθθθ Knowledge Summary