The so-called merge sort

Source: Internet
Author: User

Merge sort:

Before learning to merge sort, to understand the meaning, but if you write code to achieve, that is really not, now read the purple book on the code, now really is enlightened, see this reason is also to achieve reverse order to the O (n log n) implementation.

Idea: In fact, the essence of merge sort is two points, we are not unfamiliar with two points, often use two points to find a value, we can call this value "value" of two points (namely: our purpose), the same in the merge sort, the "added value" is no longer a value, but a process, this process is "interval merge "," Interval merging "process: because we are recursive to the left and right two sub-range, so the left and right two sub-range is ordered, then, when we merge, according to the need (in the Minimum column), then select the left and right of the interval of the minimum value comparison (and do not compare with other values, here to reduce the computational In fact, the amount of consolidation here becomes the O (N) level (you can understand it carefully)). The degree of complexity of the dichotomy is log N. So the total time complexity is n log n. Haha, write write, on the merger sort completely understand, it seems to have to write more blog ah ...


Attach a merge sort code (from small to Large):

#include <iostream>
#include <cstdio>
#define MAXN 100010

using namespace std;

int A[MAXN],T[MAXN];
int N;

void Merge_sort (int l,int R) {//actually is two points, but after two points also merge
    if (r-l<=0) return;
    int m= (L+R)/2;
    Merge_sort (l,m);//This is used on both sides are closed interval
    merge_sort (m+1,r);//Both sides are closed interval
    int i=l,j=m+1,cnt=0;
    while (I<=m | | j<=r) {//merge sort is centered on the merging of intervals
        if (i>m | | (J<=r && a[j]<a[i])) T[cnt++]=a[j++];
        else t[cnt++]=a[i++];//the result into the auxiliary array
    }
    for (i=0;i<cnt;i++) a[i+l]=t[i];//The result into the element group, you can reduce the memory
}

int main () {while
    (cin>>n) {for
        (int i=0;i<n;i++) cin>>a[i];
        Merge_sort (0,n-1);
        for (int i=0;i<n-1;i++) cout<<a[i]<< "";
        cout<<a[n-1]<<endl;
    }
    return 0;
}


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.