# "Chaos" algorithm -- merge and sort

Source: Internet
Author: User

Disclaimer: copyright. You are welcome to reprint it. Contact mailbox: yiluohuanghun@gmail.com]

The basic idea of merging sorted Merge sort is to Merge two ordered tables with two sorted ascending orders.) The sequence is stored in the adjacent position of the same array. It may be set to A [l .. m], A [m + 1 .. h], merge them into an ordered series, and store them in A [l .. h].

The time complexity of Merge Sorting is O (nlog2n) in both the best and worst cases, and the space complexity is O (N ), it is a stable sorting method. In the worst case, Merge Sorting is the time complexity of O (NlogN). The disadvantage is that there is an extra space of O (N) in Merge.

In general, you can use the divide-and-conquer method to solve the Sorting Problem. This problem is to arrange n elements in a non-descending order. The divide and conquer method usually uses the following steps to sort the algorithm: If n is 1, the algorithm ends; otherwise, this element set is divided into two or more child sets, sort each sub-set separately, and then combine the sub-sets in the sorted order into a set.

Assume that only the set of n elements is divided into two child sets. Now you need to determine how to divide sub-sets. One possibility is to place the first n-1 element in the first sub-set as A), and the last element in the second sub-set as B ). Recursively sorts A in this way. B only contains one element, so it has been sorted. After sorting A, you only need to use the program function insert to merge A and B. By comparing this sort algorithm with InsertionSort, we can find that this sort algorithm is actually a recursive algorithm for inserting the sort. The complexity of this algorithm is O (n2 ). Another way to divide n elements into two child sets is to put the elements with the maximum value into B, and the rest into. Then A is recursively sorted. To merge the sorted values of A and B, you only need to add B to. If we use the M a x function to find the largest element, this sort algorithm is actually a recursive algorithm of SelectionSort.

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1621261340-0.png "title =" .png "/>

Divide governance:

Break down the original problem into several subproblems of small scale but similar to the original problem, solve these subproblems recursively, and then combine the solutions of these subproblems to establish the solution of the original problem, merge Sorting completely follows the sub-governance mode:

Decomposition: breaks down the sequences of n elements to be sorted into two subsequences with n/2 elements.

Solution: Use Merge Sorting to recursively sort two subsequences

Merge: merge two sorted subsequences to generate sorted answers.

Merge Sorting Algorithm:

The key operation of the Merge Sorting Algorithm is to merge the two sorted sequences in the merge step. We call an auxiliary process merge (A, p, q, r, T) to complete the merge, where A is an array, p, q, and r is the array subscript, p <= q <r, T is an auxiliary array space. This process assumes that the sub-arrays A [p. q] And A [q + 1. r] are sorted in order. It combines the two sub-arrays to form A single sorted sub-array and replaces the current sub-array A [p. r] n

The merge process requires O (n) time, where n = r-p + 1 is the total number of elements to be merged.

// 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; // sort the results one by one and save the results in the corresponding Temporary array while (I <= m & j <= n) {if (a [I] <= a [j]) temp [k ++] = a [I ++]; else temp [k ++] = a [j ++];} // The second half has been traversed, and the rest of the first half has been directly copied while (I <= m) temp [k ++] = a [I ++]; // The first half has been traversed, and the other half has been directly copied while (j <= n) temp [k ++] = a [j ++]; // copy the data stored in the temporary array to the original array for (I = 0; I <k; I ++) a [first + I] = temp [I];}

Now, we can use merge as a sub-program in the merge Sorting Algorithm. In the following process, mereg_sort (A, p, r) sorts the elements in the sub-array A [p... r. If p> = r, the sub-array can have at most one element, so the order has been sorted. Otherwise, the decomposition step simply calculates A subscript q and converts A [p... r] is divided into two sub-arrays A [p... q] And A [q... r]. The former contains n/2 elements, and the latter contains n/2 elements.

Void mergesort (int a [], int first, int last, int temp []) {if (first <last) {int mid = (first + last)/2; mergesort (a, first, mid, temp); // sort the first Half of mergesort (a, mid + 1, last, temp ); // sort the second half of mergearray (a, first, mid, last, temp); // combine the first and second parts} int 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 ;}

Of course, at this time I will say the same thing, the test program must be written, and the test program must be completed within the controllable scope of the program.

int main(int argc, char* argv[]){    int i, array[] = {6, 2, 3, 1, 4, 5};    MergeSort(array, 6);    for (i = 0; i < 6; i++)    {        printf("%d\t", array[i]);    }    getchar();    return 0;}

The next article seems to have sorted the heap, but I have never understood the heap sorting too well, but I do not understand the actual mechanism. I will write it later.

Related Keywords:
Related Article

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.