Implementation of Merge Sorting in the Classical Vernacular algorithm series

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.

First, consider how to merge the two ordered series. This is very easy. We only need to take the first number from the first number of two series than the limit. If we are small, we will take the first number and then delete the number from the corresponding series. Then perform the comparison. If the number of columns is null, you can directly extract the data of another series in sequence.

// Merge the ordered arrays A [] and B [] into void memeryarray (int A [], int N, int B [], int m, int C []) {int I, j, k; I = J = k = 0; while (I <n & J <m) {if (a [I] <B [J]) C [k ++] = A [I ++]; elsec [k ++] = B [J ++];} while (I <n) C [k ++] = A [I ++]; while (j <m) C [k ++] = B [J ++];}

It can be seen that the efficiency of merging ordered series is higher than that of limit, which can reach O (n ).

After solving the preceding merge ordered series problem, let's look at Merge Sorting. The basic idea is to divide the array into two groups A and B. Assume that the data in these two groups is ordered, this makes it easy to sort the two groups of data. How can we make the data in these two groups orderly?

Group A and Group B can be further divided into two groups. And so on. When the split group only has one data, we can feel that the group has reached an order, and then merge the two adjacent groups. In this way, the merging order is completed by recursively decomposing the series and then merging the series.

// Merge two ordered Series A [first... mid] And a [mid... last. Void mergearray (int A [], int first, int mid, int last, int temp []) {int I = first, j = Mid + 1; int M = mid, N = last; int K = 0; while (I <= M & J <= N) {if (a [I] <= A [J]) temp [k ++] = A [I ++]; elsetemp [k ++] = A [J ++];} while (I <= m) temp [k ++] = A [I ++]; while (j <= N) temp [k ++] = A [J ++]; for (I = 0; I <K; I ++) A [first + I] = temp [I];} void mergesort (int A [], int first, int last, int temp []) {If (first <last) {int mid = (first + last)/2; mergesort (A, first, mid, temp ); // ordered mergesort (A, Mid + 1, last, temp) on the left; // ordered mergearray (A, first, mid, last, temp) On the right ); // merge two more ordered series} bool mergesort (int A [], int N) {int * P = new int [N]; If (P = NULL) return false; mergesort (A, 0, n-1, P); Delete [] P; return true ;}

 

The efficiency of merging and sorting is higher than that of merging. Setting the length of a series to N, splitting the series into small series requires a total of n logn steps. Each step is a process of merging ordered series, the time complexity can be recorded as O (N), so a total of O (N * logn ). Since the Merge Sorting operations are performed on adjacent data each time, the Merge Sorting methods in O (N * logn) (high-speed sorting, Merge Sorting, Hill sorting, and heap sorting) it is also highly efficient.

 

Sort bubbles on my computer, insert directly to sort, merge and sort, and directly use the qsort () of the system for comparison (all under the release version)

Perform a trial of 20000 random data records:

Perform a trial of 50000 random data records:

Try again for 200000 random data records:

 

Note: In some books, a temporary array is allocated when mergearray () merges ordered series, but too many new operations will take a long time. So we made a small change. Only a new temporary array in mergesort. All subsequent operations share this temporary array.

 

 

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.