Merge Sorting (implemented in C)

Source: Internet
Author: User

The basic mode is as follows:

Decomposition: break down a problem into subproblems similar to the original problem.

Solution: recursive solutions to various subproblems

Merge: the result of merging sub-problems is resolved by the original problem.

Now we will use recursive algorithms to solve the merging and sorting with the preceding sub-governance idea.

Merge Sorting (non-descending)

Decomposition: splits Merge Sorting into two subproblems.

Pseudocode:

Copy codeThe Code is as follows: MERGE_SORT (A, begin, end)

If begin <end

Then mid <-int (begin + end)/2)

MERGE_SORT (A, begin, mid)

MERGE_SORT (A, mid + 1, end)

MERGE (A, begin, mid, end)

Solution: recursively solve each subproblem. Each subproblem continues to call itself recursively until the "begin <end" condition is not met, that is, when "begin = end, at this time, there is only one element, which is obviously ordered, so that the next merge is performed.

Merging: There is an implicit problem in the results of the merging sub-problems, that is, each sub-problem is already sorted (from the two nitrogen element sequences ). The practice is to compare the write final result of the first element of the two subsequences, and then compare them, as shown in:

Figure: the array to be sorted is 2 4 6 1 3 5

Store 2, 4, 6, and 1, 3, 5 in an array, and compare the first element smaller than the two arrays to store them in a large array until the two small arrays have 32767 elements.

Here, the 32767 flavor is infinite, because the int type in C language is 32 bits, which indicates the range is-32768---32768. Using infinity as the target can reduce the determination of whether the two small arrays are empty. With the target, the number of elements in the large array can be directly determined.

The execution process is as follows:

Decomposition + execution is from top to bottom, and merge is from bottom to top.

Code:

Copy codeThe Code is as follows: # include <stdio. h>

Void MERGE (int * A, int B, int m, int e)

{

Int l = m-B + 1, r = e-m, I;

Int L [l + 1], R [r + 1];

For (I = 0; I <l; I ++)

{

L [I] = A [B + I];

}

For (I = 0; I <r; I ++)

{

R [I] = A [m + I + 1];

}

L [l] = 32767;

R [r] = 32767;

L = 0;

R = 0;

For (I = 0; I <e-B + 1; I ++)

{

If (L [l] <R [r])

{

A [B + I] = L [l];

L ++;

}

Else {

A [B + I] = R [r];

R ++;

}

}

}

Void MERGE_SORT (int * A, int B, int e)

{

If (B <e)

{

Int m = (B + e)/2;

MERGE_SORT (A, B, m );

MERGE_SORT (A, m + 1, e );

MERGE (A, B, m, e );

}

}

Int main ()

{

Int A [500];

Int lens, I;

Printf ("Please Enter the lenghth of array :");

Scanf ("% d", & lens );

Printf ("Please Enter the elements of the array :");

For (I = 0; I <lens; I ++)

Scanf ("% d", & A [I]);

MERGE_SORT (A, 0, lens-1 );

Printf ("the result of the sort is: \ n ");

For (I = 0; I <lens; I ++)

{

Printf ("% d", A [I]);

}

Return 0;

}

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.