Introduction to 9.8.1 Merge sort

We talked about heap sequencing, because it used a complete binary tree, fully utilizing the depth of the complete binary tree is the ⌊log2n⌋+1 characteristic, so the efficiency is high. But the design of the heap structure itself is more complex, to be honest, can come up with such a structure is very difficult, there is no more direct and simple way to use the complete binary tree to sort it. Of course there is.

Let me give you an example first. Do you know how to divide the college entrance examination into one, two, and the specialized fraction?

Simply put, if the undergraduate majors in a province of high school science students plan to recruit 10,000, then the province to participate in the college entrance examination of Science students ranked, the 10,000th is the total number of undergraduate students of the score (the reality may be more complex, here Simplified). In other words, even if you are your class first, even grade number one, if you do not have the score, then you are not ranked the top 10,000 in the province, you will basically lose the chance of undergraduate.

In other words, the so-called provincial rankings, in fact, each city, each county, each school, each class ranked after the merger and then ranked. Notice that I use the word merge here.

It is easy for us to compare the scores of two students, such as the low ratio of a and b, and the low C-butyl score. Then we can easily get the result of the combination of B-C, in the same way, the rank of E-Geng is also easy to get, because their two groups are orderly, and their eight students to combine the results of the order is very easy to do, continue to go on ... Finally completed the provincial students ranked, at this time the college entrance examination was born.

To get a clearer idea of what's going on here, let's look at figure 9-8-1, and we'll have this unordered array sequence {16,7,13,10,9,15,3,2,5,8,12,1,11,4,6,14}, sorted by 22 merging, then merging, and finally getting an ordered array. Careful observation of its shape, you will find that it is like an inverted complete binary tree, usually involves a complete binary tree structure of the sorting algorithm, the efficiency is generally not low-this is what we want to talk about the merging of the sorting method.

9.8.2 Merging sorting algorithm

The Chinese meaning of the term "merging" is the meaning of merging, incorporating, and the definition in data structure is to combine two or more ordered tables into a new ordered table.

Merge sort (merging sort) is a sort method that is realized by merging ideas. The principle is that if the initial sequence contains n records, it can be regarded as N ordered subsequence, the length of each subsequence is 1 and then 22 is merged to obtain an ordered sequence of 2 or 1 of the length of the ⌈n/2⌉ (⌈x⌉ representing the smallest integer not less than x), and then 22 merges, ..., so repeated, Until a sequential sequence of length n is obtained, this sort method is called the 2-way merge sort.

Well, with the initial understanding of the merge sort, let's look at the code.

/* The sequential table L for merge sort
/void MergeSort (SqList *l)
{
msort (l->r,l->r,1,l->length);
}

A code, don't be surprised, it just calls another function. In order to unify with the previous sorting algorithm, we used the same parameter definition SqList *l, because we want to explain the merge sort implementation need to use recursive call, so we encapsulate a function. Suppose you want to sort the array {50,10,90,30,70,40,80,60,20} now, l.length=9, I'm going to look at the Msort implementation.

/* Will sr[s. T] Merge sort for tr1[s. T] *
/void msort (int sr[],int tr1[],int s, int t)
{
int m;
int tr2[maxsize+1];
if (s==t)
tr1[s]=sr[s];
else
{
m= (s+t)/2; /* Will sr[s. T] split into Sr[s. M] and sr[m+1..t] *
/Msort (SR,TR2,S,M);/* recursively sr[s. M] Merge into ordered Tr2[s. M] *
/Msort (SR,TR2,M+1,T);/* recursively merge sr[m+1..t] into ordered tr2[m+1..t]
/merge (tr2,tr1,s,m,t); * Will tr2[s. M] and tr2[m+1..t] merge to Tr1[s. T] */
}
}

1 The SR and TR1 are {50,10,90,30,70,40,80,60,20},s=1,t=9 when msort is called, and ultimately our aim is to order the arrays in TR1.

2 Line 5th, obviously S is not equal to T, execute line 8th to 13th statement block.

3) Line 9th, m= (1+9)/2=5. M is the middle subscript of the sequence.

4 at this point, line 10th, invoke "Msort (sr,tr2,1,5);" The goal is to merge the 1th to 5th keyword in the array SR into an ordered TR2 (before the call TR2 an empty array), line 11th, and invoke "Msort (sr,tr2,6,9);" The goal is to merge the 6th to 9th keyword in the array sr into an ordered TR2. That is, the code is ready to divide the array into two groups before calling the two lines of code. As shown in Figure 9-8-2.

5 line 12th, the code details of the function merge will be spoken again, calling "merge (tr2,tr1,1,5,9);" The goal in fact is to the 10th and 11 lines of code to obtain the array TR2 (note that it is subscript for 1~5 and 6~9 of the keywords are ordered respectively) into TR1, at this time the equivalent of the entire sort has been completed. As shown in Figure 9-8-3.

6 again to see the 10th line recursive call in, s=1,t=5,m= (1+5)/2=3. This is the equivalent of 5 records to three and two. Continue recursively, until subdivided into a record to fill the TR2, where s is equal to T, recursively returned, as shown on the left of the figure 9-8-4. Each recursive return executes the 12th row of the current recursive function, merging the TR2 into the TR1. As shown in Figure 9-8-4 right. Finally make the current sequence orderly.

7 the same 11th line is similar to the same way, as shown in Figure 9-8-5.

8 at this point is the last time we have executed the 12th line of code, the {10,30,50,70,90} and {20,40,60,80} merged into a final ordered sequence.

It can be said that the Msort function is well understood if the functions of the recursive function are understood in a more transparent way. Let's take a look at the entire Data transformation diagram, as shown in Figure 9-8-6.

Now let's look at how the code for the merge function is implemented.

/* will be ordered sr[i ... M] and SR[M+1..N] Merge into ordered Tr[i. N] *
/void Merge (int sr[],int tr[],int i,int m,int N)
{
int j,k,l;
For (j=m+1,k=i;i<=m && j<=n;k++)/* To merge the records from the SR into the TR
/{
if (Sr[i]<sr[j])
tr[k]=sr[i++]
from small to large; else
tr[k]=sr[j++];
}
if (i<=m)
{for
(l=0;l<=m-i;l++)
tr[k+l]=sr[i+l]; /* The remaining sr[i ... M] Copy to TR/
}
if (j<=n)
{for
(l=0;l<=n-j;l++)
tr[k+l]=sr[j+l]; /* The remaining sr[j ... N] Copy to TR */
}
}

1) Suppose the merge we call at this point is to merge {10,30,50,70,90} and {20,40,60,80} into the final ordered sequence, so the array sr is {10,30,50,70,90,20,40,60,80},i=1,m =5,n=9.

2) line 4th, for Loops, J begins with M+1=6 to 9,i from 1 to 5,k starting from 1 each time the 1,k value is used for the subscript of the destination array tr.

3) line 6th, sr[i]=sr[1]=10,sr[j]= Sr[6]=20,sr[i]<sr[j], execute line 7th, tr[k]=tr[1]=10, and i++. As shown in Figure 9-8-7.

4) loop again, k++ get k=2,sr[i]=sr[2]=30,sr[j]= sr[6]=20,sr[i]>sr[j], execute line 9th, tr[k]=tr[2]=20, and j+ +, as shown in Figure 9-8-8.

5) cycle again, k++ get k=3,sr[i]=sr[2]=30,sr[j]= sr[7]=40,sr[i]<sr[j], execute line 7th, tr[k]=tr[3]=30, and i+ +, as shown in Figure 9-8-9.

6) Next exactly the same operation, until J + +, j=10, greater than 9 exits the loop. As shown in Figure 9-8-10. The code in the

7) Line 11th to 20th, in fact, will merge the remaining array data and move to the back of tr. Current k=9,i=m=5, executes line 13th to 20th code, for Loop l=0,tr[k+l]=sr[i+l]=90. Done.

in this way, our merge sort even completes a sort work, how, and heap sort ratio, is not to be simpler. Source: http://www.cnblogs.com/cj723/archive/2011/04/25/2026751.html