Analysis of the complexity of 9.8.3 merging order
Let's analyze the time complexity of the merge sort, which requires a 22 merge of ordered sequences with the adjacent length of h in Sr[1]~sr[n]. And put the results in tr1[1]~tr1[n], this requires scanning all the records in the sequence to be sorted so that the O (n) time is consumed and, by the depth of the complete binary tree, the entire merge sort needs to be ⌈log2n⌉, so the total time complexity is O (NLOGN), And this is the best, worst, average time performance in the merge sort algorithm.
Because the merging sequence needs the same amount of storage space as the original record sequence to store the merging result and the stack space with log2n depth, the space complexity is O (N+LOGN).
In addition, a careful study of the code, found in the merge function there is an if (SR[I]<SR[J) statement, which means that it requires 22 comparisons, there is no jump, so merge sort is a stable sorting algorithm.
In other words, merge ordering is an efficient and stable algorithm that is more memory intensive.
Merging sort by 9.8.4 non-recursive implementation
We often say, "No best, only better." "Merge ordering is a lot of references to recursion, and although it's clear and easy to understand on the code, it can result in both time and space performance loss." The pursuit of our ranking is efficiency, it is possible to convert recursion into iterations. The conclusion is of course possible, and the performance is further improved after the change. Look at the code.
/* The sequential table L is not recursively sorted
/void MergeSort2 (SqList *l)
{
int* tr= (int*) malloc (l->length * sizeof (int));/* Apply for extra space *
/int k=1;
while (K<l->length)
{
mergepass (l->r,tr,k,l->length);
K=2*k; /* subsequence Length doubling * * *
mergepass (tr,l->r,k,l->length);
K=2*k; /* Sub-sequence length double/
}
}
1 The program starts executing, the array L is {50,10,90,30,70,40,80,60,20},l.length=9.
2 in line 3rd, we applied for additional array memory space in advance to store the merged results.
3 the 5th to 11th line, is a while loop, the purpose is to continuously merge ordered sequence. Note that the K-value changes, line 8th and 10th, in the continuous cycle, it will be 1→2→4→8→16, jump out of the loop.
4 The 7th line, at which point the K=1,mergepass function merges the original unordered array 22 into TR, which is later said in the function code. As shown in Figure 9-8-11.
5) line 8th, k=2
6 Line 9th, the Mergepass function merges the ordered sequence of 22 merges in TR back into the array l.r, as shown in Figure 9-8-12.
7 line 10th, k=4, because the k<9, so continue to cycle, merge again, the final execution of the 7th to 10th line, k=16, end loop, complete the sorting work. As shown in Figure 9-8-13.
From the code, we can feel that the recursive iterative approach is more straightforward, starting with the smallest sequence and merging up to completion. You do not need to merge recursive algorithms, you need to split the recursion, and then merge exit recursion.
Now let's see how the Mergepass code is implemented.
/* To merge the subsequence 22 of the adjacent length in sr[] to the tr[]
/void mergepass (int sr[],int tr[],int s,int N)
{
int i=1;
Int J;
while (i <= n-2*s+1)
{
merge (sr,tr,i,i+s-1,i+2*s-1);/* 22 Merge */
i=i+2*s;
}
if (i<n-s+1)/ * Merges the last two sequences
/merges (sr,tr,i,i+s-1,n);
else /* If only the last single subsequence is left
/for (J =i;j <= n;j++)
tr[j] = sr[j];
}
1) program execution. The first time we call "Mergepass (l.r,tr,k,l.length)," where L.R is an initial unordered state, TR is an empty array of new requests, k=1,l.length=9.
2 line 5th to 9th, the purpose of the loop is 22 merge, because s=1,n-2*s+1=8, why loop i from 1 to 8, instead of 9. is because 22 merge, the final 9 records will be left, cannot merge.
3 The 7th line, the merge function we have discussed in detail before, at this time i=1,i+s-1=1,i+2*s-1=2. In other words, we merge the first and second records in the SR (that is, L.R) into tr. Then line 8th, i=i+2*s=3, and then loop, we just merge the third and fourth records into TR, until the seventh and eighth records are merged. As shown in Figure 9-8-14.
4 the 10th to 14th line, mainly to deal with the final mantissa, line 11th is to say that the last remaining multiple records into the TR. However, because of I=9,n-s+1=9, the 13th to 14th line is executed, putting 20 at the end of the TR array.
5) When calling Mergepass again, s=2, the 5th to 9th line of the loop, from the 8th row of i=i+2*s know, at this time I was in 4 for the loop. In other words, two ordered sequences with two records are merged into an ordered sequence of four records. Finally, the last remaining nineth record "20" is inserted into tr.
6) followed by a similar, slightly.
Non-recursive iterative methods to avoid the recursive depth of log 2n stack space, the space is only used to request the temporary use of the TR array, so the space complexity of O (n )。 And to avoid recursion also in the time performance of a certain increase, it should be said that the use of merge sorting, as far as possible to consider the use of non-recursive methods. Origin: Http://www.cnblogs.com/cj723/archive/2011/04/26/2026886.html