Introduction to Sorting Algorithm Implementation (Merge Sorting)

Source: Internet
Author: User

The Merge Sorting method combines two (or more) ordered tables into a new ordered table, that is, the sequence to be sorted is divided into several subsequences, each of which is ordered. Then combine the ordered subsequences into the overall ordered sequence. Merge Sorting is an effective Sorting Algorithm Based on merge operations. This algorithm is a very typical application of Divide and Conquer. Merges ordered subsequences to obtain a fully ordered sequence. That is, first orders each subsequence, and then orders the subsequence segments. If two ordered tables are merged into an ordered table, it is called a 2-way merge. First, consider how to merge the two ordered series. This is very simple, as long as we compare the first number of two columns, who is the first to take the first, and then delete the number in the corresponding series. Then compare the data. If the number of columns is null, extract the data of the other series in sequence. [Cpp] // merge the ordered arrays a [] and B [] into c [] 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 ++]; else c [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 relatively high, which can reach O (n ). The preceding merge sequence problem is solved. Let's look at the merge sequence. The basic idea is to divide the array into two groups A and B. If 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 has only one data, you can think 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. [Cpp] // 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 ++]; else temp [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); // left ordered mergesort (, mid + 1, last, temp); // right ordered mergearray (a, first, mid, last, temp ); // 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 merge sorting algorithms is much faster than the previous descriptions (insert, bubble, exchange, and fast. However, an additional declarative space is required to store the merged data, while there is still a lot of fast sorting in the application. You can select an efficient sorting method based on your individual needs.

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.