Sort--mergesort Merge sort?

Source: Internet
Author: User
Tags comparable

  MergeSort ' s Implementation

The central idea of mergetsort is to divide and conquer ideas and solve big problems by solving every small problem.

Let's say you have 2 sorted arrays

arrays [4] [8] and array [5] [7] are sorted

4 is the smallest value of the first array and the second array with the smallest value 5 to compare 4 < 5 so the smallest element is 4

After the first element is finished [4] [NULL] [NULL] [NULL]

Then the second element, 8 and 5, is compared. 5 relatively small

So it is [4] [5] [NULL] [NULL], then the first array with the smallest value of 8 and the second array is the best value of 7 compared. 7 < 8

So it becomes [4] [5] [7] [8].

So the key to the problem is to first sort the 2 arrays separately. and merge into a large array. Steal a picture of others below. Quack Gaga

    

The complexity of MergeSort is NLGN. Pretty fast algorithm. And it's stable.

Here is a concrete implementation

  

    //time:n log (N) & wasting space that proportional to N     Public Static voidsort (comparable[] a) {sort (A,NewComparable[a.length], 0, a.length-1); }        //The mid is already in order,//This is the merge method .//the length of a and auxiliary arrays need to be consistent    /**I J * [ ] [] [] [] [] * Low mid hi*/    Private Static voidSort (comparable[] A, comparable[] aux,intLowinthi) {        if(Low >= hi)return; intMiddle = low + (Hi-low)/2; Sort (A, aux, low, middle); //Sort LeftSort (A, aux, middle + 1, HI);//Sort Rightmerge (A, aux, Low, middle, hi); }        /**I j * [] [] [] [] [ ] * Low mid hi */    Private Static voidMerge (comparable[] A, comparable[] aux,intLowintMiddle,inthi) {        inti =Low ; intj = Middle + 1;  for(intK = low; K <= Hi; k++) Aux[k]= A[k];//copy elements to a secondary array                 for(intK = low; K <= Hi; k++) {                        //the first 2 judging conditions must be in front. //Otherwise, there will be a null pointer exception when comparing//because it's possible that J has gone beyond the size of hi.            if(i > middle) a[k] = aux[j++]; Else if(J > Hi) a[k] = aux[i++]; Else if(Less (aux[i],aux[j])) a[k] = aux[i++];//            ElseA[k] = aux[j++]; }    }
Merge Sort

Https://github.com/Cheemion/algorithms/blob/master/src/com/algorithms/sort/MergeSort.java

Sort--mergesort 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.