Principle Analysis and code implementation of merge sorting algorithms

Source: Internet
Author: User

Merge Sorting is an effective Sorting Algorithm Based on merge operations. This algorithm is a typical application of Divide and Conquer. Merge and sort two sorted tables into one table.

Basic Principles of Merge Sorting

Sort multiple ordered node sequences by merging them.

Merging refers to merging several sorted parts into an ordered part.

 

Basic idea of merging and sorting

Set two ordered subsequences (equivalent to the input sequence) to the adjacent positions in the same sequence: array [low .. m], array [m + 1 .. high], first merge them into a local temporary storage sequence temp (equivalent to the output sequence), and then copy temp back to array [low .. to complete the sorting.


In the specific merging process, set the I, j, and p pointers, and their initial values point to the starting positions of the three record areas respectively. When merging, compare the keywords of array [I] and array [j] in sequence, and copy the records with small (or large) keywords to temp [p, then add the pointer I or j of the copied record to 1 and the pointer p pointing to the copy position to 1. Repeat this process until one of the two input subsequences has been completely copied (it may be called null ), in this case, copy the remaining records from another non-empty subsequence to array in sequence.


If two ordered tables are merged into an ordered table, it is called a 2-way merge.

 

Example: "sorting process of Merge Sorting"

Let's look at the two sort processes of Merge Sorting below.


1. columns to be sorted)

Suppose we have a sequence without sorting out the sequence, then we first use the segmentation method to divide the sequence into subsequences with sorted orders. Then, use the merge method to merge sub-sequences into sorted sequences. The following figure shows the process of splitting and merging.

 

 

First "split" and then "merge"

 


It can be seen that, first, we divide an unordered sequence into two parts from the center, and then divide the two parts into four parts, which are separated in sequence until they are split into data one by one, then we merge the data into two pairs to make it orderly and non-stop, and finally form a sorted sequence.

 


2. columns to be sorted (, 86)

 

 

 

Source code for Merge Sorting:
Static Ret merge_sort_impl (void ** storage, void ** array, size_t low, size_t mid, size_t high, DataCompareFunc cmp)
{
Size_t I = low;
Size_t j = low;
Size_t k = mid;

If (low + 1) <mid)
{
Size_t x = low + (mid-low)> 1 );
Merge_sort_impl (storage, array, low, x, mid, cmp );
}

If (mid + 1) {
Size_t x = mid + (high-mid)> 1 );
Merge_sort_impl (storage, array, mid, x, high, cmp );
}


While (j <mid & k {
If (cmp (array [j], array [k]) <= 0)
{
Storage [I ++] = array [j ++];
}
Else
{
Storage [I ++] = array [k ++];
}
}

While (j <mid)
{
Storage [I ++] = array [j ++];
}

While (k {
Storage [I ++] = array [k ++];
}

For (I = low; I {
Array [I] = storage [I];
}

Return RET_ OK;
}

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.