Sort (v) Merge sort

Source: Internet
Author: User

definition : Sorting is accomplished by merging two ordered sequences into a large ordered sequence.

Merge sort is a typical divide-and-conquer algorithm: first divides the sequence into two parts, then iterates through each part, then merges the results one by one.

The biggest advantage of merge sorting is that it has a time complexity of O (NLGN). He is also a sort of stability, that is, the relative position of equal elements in the sequence will not change before and after the sort. His only drawback is the need to use an extra n of space to sort.

If you take the sort 38,27,43,3,9,82,10 as an example, you can see the merge sort as:

    voidCombin (vector<int>& vector,vector<int>& tmp,intLeftintMiddle,intRight//Merge two ordered intervals    {        intStart1 =Left ; intStart2 = middle +1; intindex =Left ;  while(Start1! = middle +1&& Start2! = right +1) Tmp[index+ +] = Vector[start1] < Vector[start2]? vector[start1++]: vector[start2++]; if(Start1 = = middle +1)        {             while(Start2! = right +1) Tmp[index+ +] = vector[start2++]; }        Else        {             while(Start1! = middle +1) Tmp[index+ +] = vector[start1++]; }         while(Left! = right +1) {Vector[left]=Tmp[left]; Left++; }                }    voidSection (vector<int>& Vector, vector<int>& tmp,intLeftintRight//Sort by Segment    {        intMiddle = left + (right-left)/2; if(Middle-left +1> -) //Optimization 1 section (Vector,tmp,left, middle); ElseInsertsort (Vector, left, middle); if(Right-middle > -) section (Vector,tmp, Middle+1, right); ElseInsertsort (Vector, Middle+1, right);
     if (Vector[middle] > Vector[middle+1]) //Optimization 2 Combin (Vector,tmp, left, middle, right); } voidMergeSort (vector<int>&Vector) {Vector<int>tmp; Tmp.resize (Vector.size ()); Section (Vector, TMP,0, Vector.size ()-1); }


Algorithm Analysis :

1. The average time complexity of the merge sort is O (NLGN)

When we mergesort the group N, it was graded, so that a tree with two forks was formed, each and every child node of the tree was n, and the depth of the tree was the number of layers lgn+1

So the complexity of time is: O (N) = (lgn+1) n=nlgn+n=nlgn

2. In the merge sort, we are going to create a secondary sorted array of size n to hold the initial array or to hold the merged array, so we need an extra space of auxiliary length of N.

So the spatial complexity is: S (n) = n

Improved:

1. When dividing into smaller sub-sequences, you can usually use Insert sort instead of merge sort

2. When the maximum value of the sequence on the left of the order is <= to the minimum value of the right sequence, the entire sequence is already sorted.

3. parallelization

use:

Merge sort and quick sort are all algorithms with time complexity NLGN, but compared to quick sort, merge sort is a sort of stability, that is, two elements with the same sort key will not change relative position before and after the whole sequence is sorted. This feature makes the merge sort the most efficient of the stability sort. The reference objects are sorted in Java, and the internal implementation of the stability ordering of Perl, C + +, and Python is the combined sort used.

Sort (v) 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.