Two-way merge sort Java implementation

Source: Internet
Author: User

Two-way merge sort:
The core of the idea when the problem in Split, and recursive call to divide the method, so that the problem is divided into the atomic problem can not be divided, and then merge, from the realization of the atomic problem, the layers are merged upward, and ultimately solve the overall problem. The so-called "divide and conquer, million flow to one"


The time complexity of the two-way merge sort is calculated as follows:

Reference: Introduction to algorithms------The solution of time complexity of recursive algorithm:

Two-Way merge Java implementation:

1  Public classMergeSort {2     3      Public Static voidMain (string[] args) {4         int[] Array = {1,8,6,7,2,4,11,17,6,48,3};5         //int [] array ={9,8,7,6,5,4,3,2,1};6         int[] temp =New int[Array.Length];7Sort (Array, temp, 0, array.length-1);8System.out.println ("After sorting:");9          for(inti = 0; i < Array.Length; i++) {TenSystem.out.print (Array[i] + ""); One         } A     } -     /** - * The total sort loop. Divide the problem into two, then merge the results of the two sub-problems after the problem is solved the * To understand the processing of sub-array boundaries.  - * If the sub-array is subscript (K,k + 1): Then decomposed to (k), (k+1).  - * such as sub-array subscript (K,K+1,K+2): Is decomposed into (k,k+1) and (k+2). Recursive call decomposition (k,k+1) -      * @paramArray +      * @paramTemp -      * @paramStart +      * @paramEnd A      */ at      Public Static voidSort (int[] Array,int[] temp,intStartintend) { -         if(Start <end) { -             intMid = (start + end)/2; -             //Resolve left sub-problem - sort (Array, temp, start, mid); -             //solve the right sub-problem inSort (Array, temp, Mid + 1, end); -             //merging two sub-issues to merge (Array,temp,start,end,mid); +         } -     } the     /** * * Merge function $      * @paramArray Primitive ArraysPanax Notoginseng      * @paramTemp Auxiliary Array -      * @paramStart Merge starting coordinates the      * @paramEnd Merge ending coordinates +      * @parammid = (start + end)/2 A      */ the      Public Static voidMergeint[] Array,int[] temp,intStartintEndintmid) { +         //TODO auto-generated Method Stub -         //I: Left dial hand of the problem; J: Operation Subscript for right sub-problem $         inti =start; $         intj = Mid + 1; -         intK =start; -          while(I <= mid && J <=end) { the             if(Array[i] <Array[j]) { -TEMP[K] =Array[i];Wuyik++; thei++; -}Else { WuTEMP[K] =Array[j]; -k++; AboutJ + +; $             } -         }     -         //left dial hand array with elements -          while(I <=mid) { ATEMP[K] =Array[i]; +k++; thei++; -         } $         //right child number or element the          while(J <=end) { theTEMP[K] =Array[j]; thek++; theJ + +; -         } in  the         //copies the ordered elements from the auxiliary array to the original array the         //Note: The auxiliary array corresponds to the original array one by one About          for(intindex = start; Index <= end; Index + +){ theArray[index] =Temp[index]; theSystem.out.println ("temp[" + Index + "]" + "..." +Temp[index]); the         } +System.out.println ("==============="); -     } the}

With the console output, we can see the sequencing process:

Temp[0] ..... 1temp[1] ..... 8===============temp[0] ..... 1temp[1] ..... 6temp[2] ..... 8===============temp[3] ..... 2temp[4] ..... 7===============temp[3] ..... 2temp[4] ..... 4temp[5] ..... 7===============temp[0] ..... 1temp[1] ..... 2temp[2] ..... 4temp[3] ..... 6temp[4] ..... 7temp[5] ..... 8===============temp[6] ..... 11temp[7] ..... 17===============temp[6] ..... 6temp[7] ..... 11temp[8] ..... 17===============temp[9] ..... 3temp[10] ..... 48===============temp[6] ..... 3temp[7] ..... 6temp[8] ..... 11temp[9] ..... 17temp[10] ..... 48===============temp[0] ..... 1temp[1] ..... 2temp[2] ..... 3temp[3] ..... 4temp[4] ..... 6temp[5] ..... 6temp[6] ..... 7temp[7] ..... 8temp[8] ..... 11temp[9] ..... 17temp[10] ..... 48===============after sorting:1 2 3 4 6 6 7 8 11 17 48

Two-way merge sort Java 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.