Merge sort: Step explanation and code implementation

Source: Internet
Author: User
Tags sorts

Merge sort

In some common sorts, merge sort can be one of the best practices in sorting in terms of time overhead (time complexity =n*log N), and today we'll look at how merging is implemented.

A merge sort can be broadly divided into two steps:

1, separates the array from the middle, and sorts the sides separately.

2 . Merge two ordered arrays.

 So the implementation of the merger sort is mainly to solve these two problems.

Is the approximate step to merge the sort, the red line represents the array is split, the Blue arrow represents the broken array is processed separately, the Red arrow represents the array after sorting back

Question 1: How do I separate the arrays and sort them separately?

Answer: Using recursive implementations

question 2: How do I sort the two ordered arrays in a merge order?

assuming that the second row, you need to merge (2,4,5,8) and (1,3,6,7) two arrays.

1, because both arrays are sequential , and only need to compare the first number of two arrays, then the smaller number is necessarily the smallest number in the two array.

Example: Comparison (2,4,5,8) of 2 and (1,3,6,7) of 1, then 1 is necessarily the smallest number in the two array.

2, put this number in a new array.

Example: Put 1 into the array a[], two arrays also have (2,4,5,8) 2 and (3,6,7)

3, repeat the 1th step again.

.....

This compares until one of the arrays ends, putting the remaining values of the other array into the array a

Then array A is a well-ordered array.

Code implementation:

 Public classMergessort {//sort function, target is the array to sort,//begin and end coordinates for the start and end of the interval to be sorted     Public voidMergeSort (intTarget[],intBeginintend) {        //returns directly when the array interval to be sorted is only one number        if((End-begin) ==0){            return ; }        //The two numbers are compared when the sorted array has two numbers.        if((end-begin==1) {sorts2b (target,begin,end); return ; }        //when the array to be sorted is greater than 2, we divide the interval of this array, sorting it separately;        intMid= (begin+end)/2;        MergeSort (Target,begin,mid); MergeSort (Target,mid+1, end); //Sort the sorted two arrays by combining the intervals//create an array to hold the sorted array, size is the interval size        inttemp[]=New int[End-begin+1]; //holds the start coordinate of the first array        intp1=begin; //holds the start coordinate of the second array        intP2=mid+1; //holds the coordinates of the last stored number of the temp array, initially 0        intP3=0;
       while(p1<=mid&&p2<=end) { //save a small number to the temp array if(target[p1]<TARGET[P2]) {TEMP[P3]=TARGET[P1]; P3++; P1++; }Else{TEMP[P3]=TARGET[P2]; P3++; P2++; } } //Check to see if there are any inserted while(p1<=mid) {TEMP[P3]=TARGET[P1]; P3++; P1++; } while(p2<=end) {TEMP[P3]=TARGET[P2]; P3++; P2++; } //replace the row array temp with the original targetSystem.arraycopy (temp, 0, target, begin, Temp.length); }
//two numbers of a and b coordinates of the array target are sorted by small to big from small to large Public voidSORTS2B (intTarget[],intBeginintend) { //if begin>end, make an Exchange if(target[begin]>Target[end]) { inttemp=Target[begin]; Target[begin]=Target[end]; Target[end]=temp; } return ; } Public Static voidMain (string[] args) {Mergessort ms=NewMergessort (); inta[]={8,-6,2,-7,9,-23,2,7,4,67,1,8}; //Note that the third parameter is the end coordinate of the array, because the array starts at 0, so the end coordinate requires the length of the array-1Ms.mergesort (a,0,a.length-1); intI=0; while(i<a.length) {System.out.print (a[i++]+" "); } } }

Code test:

    //Test Code         Public Static voidMain (string[] args) {Mergessort ms=NewMergessort (); inta[]={8,-6,2,-7,9,-23,2,7,4,67,1,8}; //Note that the third parameter is the end coordinate of the array, because the array starts at 0, so the end coordinate requires the length of the array-1Ms.mergesort (a,0,a.length-1); intI=0;  while(i<a.length) {System.out.print (a[i++]+"  "); }    }

Results:

Write this blog hope later forget when you can review, code if there is wrong place, ask the big boys to correct, thank you ~

Merge sort: Step explanation and code implementation

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.