Merge sort Learning

Source: Internet
Author: User

Merging sort is a sort method that uses the idea of merging. The principle is that assume that the initial sequence contains N records, it can be regarded as N ordered subsequences, the length of each subsequence is 1, and then merged into two, obtain the ordinal n/2 substring (the smallest integer not less than X) of an ordered subsequence with a length of 2 or 1 ,......, This is repeated until an ordered sequence with a length of N is obtained. This sorting method is called 2-way merge sorting.

Code:

 # Define Maxsize 10/* indicates the maximum number of arrays to be sorted. You can modify the value as needed */
Typedef Struct
{
Int R [maxsize + 1 ]; /* Used to store the array to be sorted. R [0] is used as a sentry or temporary variable. */
Int Length; /* Used to record the length of an ordered table */
} Sqlist;

/* Exchange the subscript of array R in L as the value of I and J. */
Void Swap (sqlist * l, Int I, Int J)
{
Int Temp = L-> r [I];
L-> r [I] = L-> r [J];
L-> r [J] = temp;
}
Void Msort (Int Sr [], Int Tr1 [], Int S, Int T );
Void Merge ( Int Sr [], Int Tr [], Int I, Int M, Int N );

// Merge and sort the sequence table L
Void Mergesort (sqlist * l)
{
Msort (L-> r, L-> r, 1 , L-> length );
}

// Sort Sr [S. T] into tr [S. T]
Void Msort ( Int Sr [], Int Tr1 [], Int S, Int T)
{
Int M;
Int Tr2 [maxsize + 1 ];

If (S = T) // If there is only one number
{
Tr1 [s] = Sr [s];
}
Else
{
M = (S + T )/ 2 ; // Divide Sr [S. T] into Sr [S. M] AND Sr [M + 1. T]
Msort (Sr, tr2, S, M ); // Recursively merge Sr [s .. m] into an 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 ); // Merge tr2 [s .. m] And tr2 [M + 1 .. t] To tr1 [s .. t]
}
}

// Merge ordered Sr [I. m] AND Sr [M + 1. N] 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 ++) // Merge Records in SR from small to large into tr
{
If (Sr [I] <Sr [J])
Tr [k] = Sr [I ++];
Else
Tr [k] = Sr [J ++];
}
If (I <= m)
{
For (L = 0 ; L <m; l ++)
Tr [K + 1 ] = Sr [I + 1 ];// Copy the remaining SR [I. m] to tr
}
If (J <= N)
{
For (L = 0 ; L <= N-J; l ++)
Tr [K + 1 ] = Sr [J + 1 ]; // Copy the remaining SR [J. N] to tr
}
}

Complexity Analysis:

Let's analyze the time complexity of Merge Sorting. For a merge, the SR [1] ~ The adjacent length in Sr [N] is the sequence of H and is merged in two. And place the result in tr1 [1] ~ In tr1 [N], this requires scanning all records in the sequence to be sorted. Therefore, it takes O (n) time, and the depth of the Complete Binary Tree is known, the whole Merge Sorting requires the merge log to take the logarithm of N for the first time at the base of 2. Therefore, the total time complexity is O (nlogn ), in addition, this is the algorithm ; "> Best, worst, and average time performance .
as the Merge Sorting process requires the same storage space as the original record sequence to store the merge result and the stack space with the depth of log2n in recursion, therefore, the space complexity is O (n + logn) . In addition, after careful study of the code, we find that the Merge function has the IF (Sr [I] skip does not exist . Therefore, Merge Sorting is a stable Sorting Algorithm .
that is to say, Merge Sorting is an algorithm that occupies memory , but is efficient and stable .

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.