Sort-merge sort

Source: Internet
Author: User

A magical recursive call, a magical array merge!

To figure out, first understand two questions: 1, double-decker how to go. 2. How to arrange the orderly merging of the arrays in good order.

On the first question, it is recommended that you take a look at http://www.cnblogs.com/PerkinsZhu/p/5668218.html and then believe that you will understand the recursive call to another level.

On the second question, do not explain, in concise language also beyond the simple beauty of code! Byte look at the code!

 Description: Recursive decomposition to a state is an array element with only one.

The two arrays prior to merging are already in an ordered state.

There is no split array in the program, just recursive decomposition of the original array according to the array subscript.

 Public voidMergeSort (int[] Array) {PrintArray ("Original array:", array); Sort (Array,0, Array.length-1); }    Private voidSortint[] Array,intLeftintRight ) {        if(Left >=Right )return; intMid = (left + right)/2;        Sort (array, left, mid); Sort (array, mid+ 1, right);    Merger (Array, left, Mid, right); }    Private voidMerger (int[] Array,intLeftintMidintRight ) { time++; int[] temp =New int[Array.Length]; intTempindex =Left ; intCenter = mid + 1; intTempleft =Left ;  while(Left <= mid && center <=Right ) {            if(Array[left] <Array[center]) {Temp[tempindex+ +] = array[left++]; } Else{Temp[tempindex+ +] = array[center++]; }        }         while(Left <=mid) {Temp[tempindex+ +] = array[left++]; }         while(Center <=Right ) {Temp[tempindex+ +] = array[center++]; }         while(Templeft <=Right ) {Array[templeft]= temp[templeft++]; } PrintArray ("First" + Time + "sub-loop sort result:", array); }

Operation Result:

Original array: 6, 3, 2, 8, 0, 9, 7, 1, 5, 4,
1th Round results: 3, 6, 2, 8, 0, 9, 7, 1, 5, 4,
2nd round results: 2, 3, 6, 8, 0, 9, 7, 1, 5, 4,
3rd round results: 2, 3, 6, 0, 8, 9, 7, 1, 5, 4,
4th round results: 0, 2, 3, 6, 8, 9, 7, 1, 5, 4,
5th round results: 0, 2, 3, 6, 8, 7, 9, 1, 5, 4,
6th round results: 0, 2, 3, 6, 8, 1, 7, 9, 5, 4,
7th round results: 0, 2, 3, 6, 8, 1, 7, 9, 4, 5,
8th Round results: 0, 2, 3, 6, 8, 1, 4, 5, 7, 9,
9th round Results: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,

It is recommended to run with 6, 3, 2, 8, 0, 9, 7, 1, 5, 4, this array!

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