Introduction to Algorithms Learning Notes (2)-merge sort

Source: Internet
Author: User

Today, we learned the algorithm of merging and sorting on the introduction of algorithms, and finished writing pseudo-code on the paper, before learning to merge but not understanding thoroughly, in

Before also has been puzzled: why clearly merge sort than the time complexity of the fast row more stable, why the library function does not merge and use the Fast row, now know the reason, because the merge sort must open extra space, and the space overhead is relatively large, the following introduction algorithm:

First, the merge sort uses the idea of divide and conquer, divides the big data into several small data, then processes the small data separately, and finally the small data

Merge into Big data.

Second, the most important feature of merge sorting is to combine two sets of sorted data into a set of ordered data, and the time of the process

The degree of impurity is O (n).

Finally, the algorithm comes out, for an array of a[n], we want to sort him out, first, we assume A[0~N/2] and a[n/2+1,n-1] are

ordered sequence,So, we can order in O (n) time, but the question is, A[0~N/2] and a[n/2+1,n-1] are not ordered sequences, so we are going to turn them into ordered sequences, how to change? We'll sort the a[0~n/2] and a[n/2+1,n-1] separately, for A[0~N/2], we use the same method as above, divide him into A[0~N/2/2] and A[N/2+1,N/2], and if both arrays are ordered sequences, Then you can merge them and return to the previous layer, so how do you judge them to be orderly? This element is ordered when there is only one element, so simply recursive to the number of elements 1 and then back to merge.

The following is proof of the correctness of the merge () (merge) function in the following code:

1. When the first iteration of the loop, I = L, A[l, i-1] is empty, is an ordered sequence (empty also an ordered sequence), and contains the smallest number of i-l=0 la[n1],ra[n2], then c1 = C2 = 0, la[c1] and RA[C2] are the smallest elements in each other's array.

2. Assuming that the first iteration of LA[C1] <= ra[c2], then LA[C1] is not yet copied to the smallest element in a, where a is the smallest element of i-l, when the execution of a[i] = La[c1], a contains i-l+1 the smallest element, Then add C1 and I for the next iteration, and if the first time LA[C1] > RA[C2], perform a similar process.

3. After the loop is over, I = r+1, at which point A contains i-l = r-l+1 the smallest element, which is exactly the l~r of all elements, and has been sequenced, proof.

Insertion_sort#include <iostream>using namespace Std;const int inf = (1<<28); void print (int* A, int n) {f    or (int i = 0; i < n; i++) {cout << a[i] << ""; } cout << Endl;}    void merge (int *a, int l, int m, int r) {int N1 = m-l+1, N2 = r-m;    int la[(const int) (N1+1)], ra[(const int) (N2+1)];    for (int i = 0; i < N1; i++) {la[i] = a[i+l];    } for (int i = 0; i < n2; i++) {ra[i] = A[m+1+i];    } LA[N1] = ra[n2] = inf;    int c1 = 0, c2 = 0;        for (int i = l; I <= R; i++) {if (La[c1] <= ra[c2]) {a[i] = la[c1++];        } else {A[i] = ra[c2++];        }}}void merge_sort (int *a, int l, int r) {if (L < r) {int m = (L + r)/2;        Merge_sort (A, L, M);        Merge_sort (A, m+1, R);    Merge (A, L, M, R);        }}int Main () {int a[10] = {43,2,53,1,8,29,52,4,8,10};    cout << "before sorted:";        Print (A, 10); Merge_sort(A, 0, 10);    cout << "after sorted:";        Print (A, 10); return 0;}




Introduction to Algorithms Learning Notes (2)-merge sort

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.