Recursive implementation of merge ordering and non-recursive implementation code _c language

Source: Internet
Author: User

Merge sort
Merge ordering is an effective sorting algorithm based on merging operation. The algorithm is a very typical application of the partition method (Divide and Conquer). It is noteworthy that merge ordering is a stable sort method. The ordered Subsequence is merged to obtain a fully ordered sequence, that is, the sequence of each subsequence is ordered, and then the sequence between the subsequence segments is ordered. If the two ordered table is merged into an ordered table, it is called 2-way merge.

Algorithm description
The merge operation works as follows:
The first step: application space, so that its size is two of the sum of the sorted sequence, the space used to store the merged sequence
Step two: Set two pointers, starting at the initial position of two sorted sequences, respectively
Step three: Compare the elements pointed to by the two pointers, select the relatively small elements into the merged space, and move the pointer to the next position

Complexity of Time:
Time complexity O (NLOGN) This is the best, worst, and average time performance of the algorithm.
Space complexity of O (n)
The number of comparison operations is between (NLOGN)/2 and Nlogn-n + 1.
The number of assignment operations is (2NLOGN). The space complexity of the merging algorithm is: 0 (N)
Merge sorting is a more efficient and stable algorithm for memory consumption.
(above excerpt from Baidu Encyclopedia)

Code implementation
Top-up implementation:
Process of merging using auxiliary arrays

Copy Code code as follows:

void mergesort (int *aux, int *data, int l, int m, int h)
{
int k=0, i=l, j=m+1;

for (k=l; k<=h; k++)
{
if (i>m) aux[k]=data[j++];
else if (j>h) aux[k]=data[i++];
else if (Data[i]<data[j]) aux[k]=data[i++];
else aux[k]=data[j++];
}
for (k=l; k<=h; k++)
DATA[K]=AUX[K];
}

The process of sorting by recursion (merging from top to bottom)
Copy Code code as follows:

void Sort (int *aux, int *data, int l, int h)
{
if (l{
int m=l+ (H-L)/2;
Sort (AUX, data, L, M);
Sort (aux, Data, m+1, h);
MergeSort (Aux,data, L, M, h);
}
}

The process of sorting by non-recursion (merging from bottom up)
Copy Code code as follows:

void Nonrermersort (int *aux, int *data, int l, int h)
{
int I=l, J;
for (i=l; i<=h; i=2*i)
{
for (j=l; j<=h-i; j+=2*i)
MergeSort (AUX, data, J, I+j-1, Min (j+2*i-1,h));
}
}

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.