The basic idea of merging sort is solved by the method of divide and cure.
There are three ideas for each layer of the divide-and-conquer model:
decomposition of the original problem is a number of sub-problems, these sub-problems are the original problem of small-scale instances.
solve These sub-problems and solve the sub-problems recursively. However, if the size of the sub-problem is small enough, it is solved directly.
The solution of the Jia Cheng original problem of merging these sub-problems.
The merge sort algorithm completely follows the divide-and-conquer mode. Intuitively, it operates as follows:
decomposition: breaks down the sequence of the twisted elements to be sorted into two sub-sequences with N/2 elements.
Workaround: sort two sub-sequences recursively by using merge sort.
Merge: merges two sorted sub-sequences to produce sorted answers. When the sequence length to be sorted is 1 o'clock, the recursion "starts to pick up", (that is, usually the recursive exit) in this case do not do any work,
Because each sequence with a length of 1 is ordered.
The pseudo-code of "and" in the merge sort
1 MERGE (A, p, Q, R)2ni-q-p+L3rti-r-Q4Let l[1. . n1+1] End r[1. . n2+1] BeNewArrays5 fori =1 to N16L[i] = a[p + I-1]7 forj = L to N28R[J] A[q +J]9l[n+1] =∞Tenr[n+1] =∞ Onei =1 Aj =1 - forK =p to R - ifL[i] <R[j] theA[K] =L[i] -i = i+1 - Else -A[K] =R[j] +j = j+1
The process merge detailed work process is as follows:
The 1th Row calculates the Subarray a[p: Q] The length n, the 2nd row computes the Subarray a[q+1 ... R] Length N2
In line 3rd, we create an array of lengths, N1+1 and n2+1, respectively L and R ("left" and "right"), and the additional positions in each array will save the Sentinels.
The For loop on line 4th to 5th copies the Subarray a[p, Q] to l[1. N1],
The For loop of line 6th to 7th a[q+1 the Subarray: R] Copy to R[1. N2],
The 8th to 9th Row will be placed at the end of the array L and R
第10-17 will be in the process of presentation
A shallow shaded position in a pack of birds their final value. The light shadow position in L and R contains the value to be copied back to a
Now we can use the process merge as a subroutine in the merge sort algorithm. The following procedure Merge-sort (A, p, R) to sort the sub-array a[p: The elements in R]. If P≥r, then the Subarray has at most one element,
So the order is already lined up, otherwise, the decomposition step simply calculates a subscript q, which will a[p. R] is divided into two sub-arrays of a[p. Q] and a[q+l ... r]. The former consists of [N/2] elements, which contain [N/2] elements.
merge_sort (A, P, r) if p < r = (p+r)/2 merge_sort (a,p,q) merge_sort (a,q+1, R) Merge (A , p, Q, R)
In order to sort the entire sequence A = (a[1]. A[2]. (..., a[n]), we perform the initial call to Merge-sort (A. 1,a. Length), here again there is a. Length=n.
Figure 2 shows the bottom-up to the operation of the procedure when n is a power of 2. The algorithm consists of the following operations: Merging sequences with only 1 items to form a sequence with a length of 2,
A sequence with a length of 2 is combined to form a sequence with a length of 4, which continues until two sequences with a length of N/2 are combined and eventually form an ordered sequence of length n.
1 PackageCom.hone.Merge;2 3 Public classMergeSort {4 /*5 * Define a function that contains three parameters, the first parameter represents the incoming array A, the second parameter represents the temporarily stored array s6 * k, indicating the original length of the array currently required to be merged7 */8 Public Static voidMergeint[] A,int[] Swap,intk) {9 intn=a.length;Ten intM=0; One inti,j; A intS1,s2,e1,e2;//variables represent the first and the end coordinates of two arrays - - /* the * Defines the relationship between two array coordinates - */ -S1=0; - while(S1+k <= n-1){ +s2=s1+K; -E1=s2-1; +E2= (s2+k-1 <= n-1)? s2+k-1:n-1; A at for(i = s1,j=s2; I <=e1 && j<= E2; m++) { - if(a[i]<=A[j]) { -swap[m]=A[i]; -i++; -}Else { -swap[m]=A[j]; inJ + +; - } to } + - //if the elements in array 2 have been merged, the array 1 is still not merged and the remaining elements are directly the //assign a value to swap * while(i<=E1) { $swap[m]=A[i];Panax Notoginsengi++; -m++; the } + A //if the elements in array 1 have been merged, the array 2 is still not merged and the remaining elements are directly the //assign a value to swap + while(j<=E2) { -swap[m]=A[j]; $J + +; $m++; - } -s1=e2+1;//forming A closing connection the } - Wuyi //if some collections cannot be divided into two arrays, they are copied directly to the swap the for(I=S1; i<n; i++,m++) -swap[m]=A[i]; Wu - } About $ //The main purpose of defining a function at this time is to provide an appropriate function interface - Public Static voidMergeSort (int[] a) { - inti; - intn=a.length; A intK=1; + int[] swap=New int[n]; the - while(K <N) { $ merge (A, swap, k); the the the for(i=0;i<n;i++) thea[i]=Swap[i]; - ink=2 *K; the } the } About
For any merge sort, the number of merges is N times, and the recursive number of any merge sort elements is about Log N, so the time complexity of the merge sorting algorithm is:O (NLOGN)
spatial complexity: But because the merge sort requires a new space to hold n data elements each time, the space complexity required is O (n)
Stability: Stable
Features: Merge sort time is efficient, but requires additional storage space, so the merge function is suitable for less data sorting.
Algorithm exercises--merge sort