Merge Sorting uses recursive and divide-and-conquer technologies to divide data sequences into smaller half-child tables, and then sort the half-child tables, finally, we use recursive steps to merge the sorted semi-subtables into an ever-increasing ordered sequence. Merging and sorting involves two steps:
1) Partition Table
2) Merge semi-child tables
First, let's discuss merging.AlgorithmThe merge algorithm puts a series of data into a vector and the index range is [first, last]. This sequence consists of two sub-tables in the sorted order and uses the index end point (MID) it is a dividing line. The following is a sequence.
, 48
Such a sequence is divided into two subsequences: 7, 10, 25, 12, 17, and 48, as shown in:
The steps for using the merge algorithm are as follows:
Step 1: Compare V [Indexa] = 7 and V [indexb] = 12. Extract the smaller V [Indexa] and put it in the temporary vector temparray. Then add Indexa to 1.
Step 2: Compare V [Indexa] = 10 and V [indexb] = 12, place the smaller 10 to the Temporary Variable temparray, and then Indexa ++;
Step 3: Compare V [Indexa] = 19 and V [indexb] = 12, store the smaller 12 to the Temporary Variable temparray, and then indexb ++;
Step 4 to Step 7: Compare and store the data according to the above rules. The following results are obtained:
Last step: add the remaining items in sub-Table B to the temporary vector temparray
Then copy the values in the temporary variables back to the vector V according to the index location, and sort the values in the vector v.
Algorithm functions:
Public void merger (INT [] V, int first, int mid, int last)
{
Queue <int> tempv = new queue <int> ();
Int Indexa, indexb;
// Set Indexa and scan subarray1 [first, mid]
// Configure indexb and scan subarray2 [mid, last]
Indexa = first;
Indexb = mid;
// Compare the values of V [Indexa] and V [indexb] without comparing the two subcriteria
// Put the small and medium values in the Temporary Variable tempv
While (Indexa <Mid & indexb <last)
{
If (V [Indexa] <V [indexb])
{
Tempv. enqueue (V [Indexa]);
Indexa ++;
}
Else
{
Tempv. enqueue (V [indexb]);
Indexb ++;
}
}
// Copying elements in the child table is not completed
While (Indexa <mid)
{
Tempv. enqueue (V [Indexa]);
Indexa ++;
}
While (indexb <last)
{
Tempv. enqueue (V [indexb]);
Indexb ++;
}
Int Index = 0;
While (tempv. Count> 0)
{
V [first + Index] = tempv. dequeue ();
Index ++;
}
}
Implement Merge Sorting. the Merge Sorting Algorithm is divided into two steps. Step 1: first divide the original data table into sorted sub-tables, and then call merger to merge the sub-tables to make them an ordered table, for example, there are the following vectors:
25, 10, 7, 19, 12, 17, 56, 30, 21
The steps for merging and sorting the sequence are as follows:
Merge Algorithm functions
Public void mergersort (INT [] V, int first, int last)
{
If (first + 1 <last)
{
Int mid = (first + last)/2;
Mergersort (v, first, mid );
Mergersort (v, mid, last );
Merger (v, first, mid, last );
}
}
The partition table and the merge sub-table of the merge algorithm are irrelevant to the sequence of the original data. Therefore, the worst case of the algorithm is the same as the average time complexity.
The following figure shows the function call of the merge algorithm.
ExampleProgram:/Files/jillzhang/mergersort.rar
-------------------------------------------------------
People are old, their heads are not easy to use, and occasionally use algorithms to train their brains, which can prevent premature aging. Haha
Jillzhang jillzhang@126.com