Introduction to Algorithms Learning Notes (2)-merge sort

Source: Internet
Author: User

today learned the algorithm introduction of the merge sorting algorithm, and finished writing pseudo-code on the paper, once learned to merge but not thorough understanding. in

Before still puzzled: why the clear merge sort is more stable than the time complexity of the fast row. Why do library functions use fast rows without merging them? Now you know why, because the merge sort must open extra space. And the space overhead is relatively large, the following introduction algorithm:

First, the merging sort uses the thought of divide and conquer. Divide big data into small pieces of data, and then process the small data separately,

Merge into Big data.

Secondly. Merge sort uses one of the most important features. 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, if we A[0~N/2] and a[n/2+1,n-1] as

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 then sort A[0~N/2] and a[n/2+1,n-1], 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 then assume that both arrays are ordered sequences, Then you can merge them together and then return to the previous layer. So how can we infer that they are orderly? This element is ordered when there is only one element. Therefore, only the number of elements that need to be recursive is 1. And then return to the merge.

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

1. When the first loop is iterated, 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], at which point c1 = C2 = 0, la[c1] and RA[C2] are the smallest elements in each other's array.

2. If LA[C1] <= RA[C2] at the time of iteration, LA[C1] is the smallest element that has not been copied to a. At this point, a contains the i-l smallest element. When running a[i] = La[c1], a contains i-l+1 smallest elements, and then adds C1 and I for the next iteration. If first time LA[C1] > RA[C2]. Run 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. It happens to be all the elements of l~r, and it is well-sequenced and proven.

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.