Merge sort algorithm (Java implementation) __ code

Source: Internet
Author: User

1. Basic Ideas

Merge sorting (merge) is the merging of two (or more) ordered tables into a new ordered table, which divides the sequences to be sorted into several subgroups, each of which is ordered. Then the ordered Subsequence is merged into the whole ordered sequence.

Merge ordering is an effective sorting algorithm based on merging operation. The algorithm is a very typical application of the partition method (Divide and Conquer). The ordered Subsequence is merged to obtain a fully ordered sequence, that is, the sequence of each subsequence is ordered, and then the sequence between the subsequence segments is ordered. If the two ordered table is merged into an ordered table, it is called 2-way merge.

The time complexity of the merge sort is O (N*LGN).

2. Working principle

Merge sort is the use of merging ideas to sort the sequence. Depending on the implementation, the merge sort includes 2 ways of "Top-down" and "Top-down".

The following picture clearly reflects the difference between "Top-down" and "top-down" merge sorting.

merge sort from bottom to top: The sorted sequence is divided into several 1-length subsequence, then the sequence 22 is merged, a number of sequential sequences of 2 are obtained, and then 22 are merged to obtain a number of ordered sequences of 4, then merge them 22, Merge directly into a series. This gets the sort result we want.

merge sort from top down : It is in reverse direction from bottom to top. It basically consists of 3 steps: Decomposition: Dividing the current interval into a split point mid = (low + high)/2; Solution: Recursively to merge and sort two a[low...mid] and A[mid+1...high]. The recursive termination condition is that the child interval length is 1. Merging: The sorted two a[low...mid] and A[mid+1...high are merged into an ordered interval A[low...high].

3, Java implementation

/** * @Comment Merge sort algorithm * @Author Ron * @Date October 26, 2017 pm 2:56:33 * @return/public class MergeSort {/** * @Comment Merge Subsequence * @Author Ron * @Date October 26, 2017 pm 5:08:11 * @param sources arrays to be sorted * @param start The starting position of a subsequence * @param mid the end of the first subsequence, the start of the second subsequence * @param end the second subsequence ending position * @return/static void Merg 
        E (int[] sources,int start,int mid,int end) {int[] temp = new int[end-start+1];//the temporary area int i = Start that summarizes two sequences;
        int j = mid+1;

        int k=0; while (I <= mid && J <= end) {if (sources[i) <= sources[j]) {Temp[k++]=source
            S[i++];
            }else{temp[k++]=sources[j++];
        } while (I <= mid) {temp[k++]=sources[i++];
        while (j <= end) {temp[k++]=sources[j++]; Assign temp to sources for (i = 0; i < K; i++) {Sources[start + i] =Temp[i];
     }/** * @Comment * @Author Ron * @Date October 26, 2017 pm 4:28:04 * @param sources arrays to be sorted * @param gap subsequence length * @return/static void Mergegroup (int[] sources,int gap) {int margedlength = 0;

        The merged sequence starts with the subscript int length = Sources.length; for (margedlength = 0; margedlength + gap*2-1 < length; Margedlength + = gap*2) {//Merge two sub sequences Mer
        GE (sources, margedlength, margedlength+gap-1, margedlength+2*gap-1); if (margedlength + gap-1 < length) {//merges the remainder of the subsequence merge (sources, Margedlength, Marge
        Dlength+gap-1, length-1);
    /** * @Comment Merge sort (top to bottom) * @Author Ron * @Date October 26, 2017 afternoon 3:42:12 * @return * *
        static void Mergesortone (int[] sources) {if (sources = = null) {return; }//group merge for (int gap=1; gap < Sources.length Gap *= 2) {MergegroupURCES,GAP);
     /** * @Comment binary merge * @Author Ron * @Date October 26, 2017 pm 5:58:42 * @param sources sequence to be sorted * @param start sequence starting position * @param end sequence Ending position * @return/static void Mergedivide (int[] Sources,int s
        Tart,int end) {if (Sources==null | |-Start >= end) {//child interval length is 1, returns return;

        int mid = (start+end)/2;
        Mergedivide (Sources,start,mid);

        Mergedivide (Sources,mid+1,end);
    Merge (sources, start, mid, end);
        public static void Main (string[] args) {int[] a = {80,30,60,40,20,10,50,70,90};
        Mergesortone (a);
        for (int i=0; i<a.length; i++) {System.out.printf ("%d", a[i]);
        } System.out.printf ("\ n");
        int[] A2 = {80,30,60,40,20,10,50,70,90};

        Mergedivide (a2,0,a2.length-1);
        for (int i=0; i<a2.length; i++) {System.out.printf ("%d", a2[i]); }
    }
}
Related Article

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.