(1) Basic idea: Merge (merge) sorting method is to combine two (or more than two) ordered tables into a new ordered table, that is, to sort the sequence into a number of sub-sequences, each sub-sequence is ordered. Then the ordered subsequence is combined into a whole ordered sequence. merge Sort (merge-sort) is an efficient sorting algorithm based on merging operation, which is a very typical application of divide-and-conquer method (Divide and Conquer). The ordered Subsequence is merged to obtain a fully ordered sequence, i.e., the order of each subsequence is ordered, and then the sequence of sub-sequences is ordered. If two ordered tables are combined into an ordered table, they are called two-way merging .
The merge process is: Compare the size of a[i] and a[j], if A[I]≤A[J], then the first ordered table elements a[i] copied to r[k], and I and K plus 1; otherwise, the second ordered TABLE element A[j] copied to R[k], and the J and K respectively add 1, This loop continues until one of the ordered tables is finished, and then the remaining elements in the other ordered table are copied to the cells in R from subscript K to subscript t. Merging sorting algorithm we usually use recursive implementation, first to sort the interval [s,t] to the midpoint of the two points, then the left sub-range, then the right sub-range is sorted, and finally the left and right intervals with a merge operation into an orderly interval [s,t].
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/83/A6/wKioL1d50hXiQGgGAAR3Z1A8M70459.jpg "title=" C8177f3e6709c93d673b9ed49d3df8dcd00054c3.jpg "width=" height= "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width : 500px;height:316px; "alt=" wkiol1d50hxiqgggaar3z1a8m70459.jpg "/>
(2) algorithm description
The merge operation works as follows:
The first step: the application space, so that its size is two sorted sequence of the sum, the space used to store the combined sequence;
The second step: set two pointers , the initial position is the starting position of two sorted sequences;
Step three: Compare the elements pointed to by two pointers, select a relatively small element into the merge space, and move the pointer to the next position;
Repeat step 3 until one pointer goes beyond the end of the sequence, copying all remaining elements of the other sequence directly to the end of the merge sequence.
(3) The code is as follows:
package structsmethod;import java.util.arrays; public class mergingsort { /* * Merge Sort */int a[]={ 49,38,65,97,76,13,27,49,78,34,12,64,5,4,62,99,98,54,56,17,18,23,34,15,35,25,53,51}; public mergingsort () { sort (a,0,a.length-1); for (int i=0;i<a.length;i++) System.out.println (A[i]); } public void sort (int[] data, int Left, int right) { if (left<right) { //Find the intermediate index int center= (left+right)/2; //recursion to the left array sort (data,left,center); //recursively sort the right array ( Data,center+1,right); //merger merge (data,left,center,right); } } public void merge ( Int[] data, int left, int center, int right) { int [] tmparr=new int[data.length]; int mid =center+1; //third records the index of the intermediate array int Third=left; int tmp=left; while ( Left<=center&&mid<=right) { // Take the smallest of the two arrays into the middle array &Nbsp; if (Data[left]<=data[mid]) { tmpArr[third++]=data[left++]; }else{ tmparr[third++]=data[mid++]; } } //the remainder into the middle array in turn while (mid<=right) { tmparr[ Third++]=data[mid++]; } while (left<= Center) { tmparr[third++]=data[left++]; } //Copy the contents of the intermediate array back to the original array while (tmp<=right) {&NBSP;&Nbsp; data[tmp]=tmparr[tmp++]; } system.out.println (arrays.tostring (data)); } Public static void main (String[] args) {mergingsort ms=new mergingsort (); Ms.sort (ms.a, 0, ms.a.length-1);}}
The sorting results are as follows:
[38, 49, 65, 97, 76, 13, 27, 49, 78, 34, 12, 64, 5, 4, 62, 99, 98, 54, 56, 17, 18, 23, 34, 15, 35, 25, 53, 51]
[38, 49, 65, 97, 76, 13, 27, 49, 78, 34, 12, 64, 5, 4, 62, 99, 98, 54, 56, 17, 18, 23, 34, 15, 35, 25, 53, 51]
[38, 49, 65, 97, 76, 13, 27, 49, 78, 34, 12, 64, 5, 4, 62, 99, 98, 54, 56, 17, 18, 23, 34, 15, 35, 25, 53, 51]
[38, 49, 65, 97, 13, 76, 27, 49, 78, 34, 12, 64, 5, 4, 62, 99, 98, 54, 56, 17, 18, 23, 34, 15, 35, 25, 53, 51]
[38, 49, 65, 97, 13, 27, 76, 49, 78, 34, 12, 64, 5, 4, 62, 99, 98, 54, 56, 17, 18, 23, 34, 15, 35, 25, 53, 51]
[13, 27, 38, 49, 65, 76, 97, 49, 78, 34, 12, 64, 5, 4, 62, 99, 98, 54, 56, 17, 18, 23, 34, 15, 35, 25, 53, 51]
[13, 27, 38, 49, 65, 76, 97, 49, 78, 34, 12, 64, 5, 4, 62, 99, 98, 54, 56, 17, 18, 23, 34, 15, 35, 25, 53, 51]
[13, 27, 38, 49, 65, 76, 97, 49, 78, 12, 34, 64, 5, 4, 62, 99, 98, 54, 56, 17, 18, 23, 34, 15, 35, 25, 53, 51]
[13, 27, 38, 49, 65, 76, 97, 12, 34, 49, 78, 64, 5, 4, 62, 99, 98, 54, 56, 17, 18, 23, 34, 15, 35, 25, 53, 51]
[13, 27, 38, 49, 65, 76, 97, 12, 34, 49, 78, 5, 64, 4, 62, 99, 98, 54, 56, 17, 18, 23, 34, 15, 35, 25, 53, 51]
[13, 27, 38, 49, 65, 76, 97, 12, 34, 49, 78, 4, 5, 64, 62, 99, 98, 54, 56, 17, 18, 23, 34, 15, 35, 25, 53, 51]
[13, 27, 38, 49, 65, 76, 97, 4, 5, 12, 34, 49, 64, 78, 62, 99, 98, 54, 56, 17, 18, 23, 34, 15, 35, 25, 53, 51]
[4, 5, 12, 13, 27, 34, 38, 49, 49, 64, 65, 76, 78, 97, 62, 99, 98, 54, 56, 17, 18, 23, 34, 15, 35, 25, 53, 51]
[4, 5, 12, 13, 27, 34, 38, 49, 49, 64, 65, 76, 78, 97, 62, 99, 98, 54, 56, 17, 18, 23, 34, 15, 35, 25, 53, 51]
[4, 5, 12, 13, 27, 34, 38, 49, 49, 64, 65, 76, 78, 97, 62, 99, 54, 98, 56, 17, 18, 23, 34, 15, 35, 25, 53, 51]
[4, 5, 12, 13, 27, 34, 38, 49, 49, 64, 65, 76, 78, 97, 54, 62, 98, 99, 56, 17, 18, 23, 34, 15, 35, 25, 53, 51]
[4, 5, 12, 13, 27, 34, 38, 49, 49, 64, 65, 76, 78, 97, 54, 62, 98, 99, 17, 56, 18, 23, 34, 15, 35, 25, 53, 51]
[4, 5, 12, 13, 27, 34, 38, 49, 49, 64, 65, 76, 78, 97, 54, 62, 98, 99, 17, 18, 56, 23, 34, 15, 35, 25, 53, 51]
[4, 5, 12, 13, 27, 34, 38, 49, 49, 64, 65, 76, 78, 97, 17, 18, 54, 56, 62, 98, 99, 23, 34, 15, 35, 25, 53, 51]
[4, 5, 12, 13, 27, 34, 38, 49, 49, 64, 65, 76, 78, 97, 17, 18, 54, 56, 62, 98, 99, 23, 34, 15, 35, 25, 53, 51]
[4, 5, 12, 13, 27, 34, 38, 49, 49, 64, 65, 76, 78, 97, 17, 18, 54, 56, 62, 98, 99, 23, 34, 15, 35, 25, 53, 51]
[4, 5, 12, 13, 27, 34, 38, 49, 49, 64, 65, 76, 78, 97, 17, 18, 54, 56, 62, 98, 99, 15, 23, 34, 35, 25, 53, 51]
[4, 5, 12, 13, 27, 34, 38, 49, 49, 64, 65, 76, 78, 97, 17, 18, 54, 56, 62, 98, 99, 15, 23, 34, 35, 25, 53, 51]
[4, 5, 12, 13, 27, 34, 38, 49, 49, 64, 65, 76, 78, 97, 17, 18, 54, 56, 62, 98, 99, 15, 23, 34, 35, 25, 51, 53]
[4, 5, 12, 13, 27, 34, 38, 49, 49, 64, 65, 76, 78, 97, 17, 18, 54, 56, 62, 98, 99, 15, 23, 25, 34, 35, 51, 53]
[4, 5, 12, 13, 27, 34, 38, 49, 49, 64, 65, 76, 78, 97, 15, 17, 18, 23, 25, 34, 35, 51, 53, 54, 56, 62, 98, 99]
[4, 5, 12, 13, 15, 17, 18, 23, 25, 27, 34, 34, 35, 38, 49, 49, 51, 53, 54, 56, 62, 64, 65, 76, 78, 97, 98, 99]
4
5
12
13
15
17
18
23
25
27
34
34
35
38
49
49
51
53
54
56
62
64
65
76
78
97
98
99
[4, 5, 12, 13, 15, 17, 18, 23, 25, 27, 34, 34, 35, 38, 49, 49, 51, 53, 54, 56, 62, 64, 65, 76, 78, 97, 98, 99]
[4, 5, 12, 13, 15, 17, 18, 23, 25, 27, 34, 34, 35, 38, 49, 49, 51, 53, 54, 56, 62, 64, 65, 76, 78, 97, 98, 99]
[4, 5, 12, 13, 15, 17, 18, 23, 25, 27, 34, 34, 35, 38, 49, 49, 51, 53, 54, 56, 62, 64, 65, 76, 78, 97, 98, 99]
[4, 5, 12, 13, 15, 17, 18, 23, 25, 27, 34, 34, 35, 38, 49, 49, 51, 53, 54, 56, 62, 64, 65, 76, 78, 97, 98, 99]
[4, 5, 12, 13, 15, 17, 18, 23, 25, 27, 34, 34, 35, 38, 49, 49, 51, 53, 54, 56, 62, 64, 65, 76, 78, 97, 98, 99]
[4, 5, 12, 13, 15, 17, 18, 23, 25, 27, 34, 34, 35, 38, 49, 49, 51, 53, 54, 56, 62, 64, 65, 76, 78, 97, 98, 99]
[4, 5, 12, 13, 15, 17, 18, 23, 25, 27, 34, 34, 35, 38, 49, 49, 51, 53, 54, 56, 62, 64, 65, 76, 78, 97, 98, 99]
[4, 5, 12, 13, 15, 17, 18, 23, 25, 27, 34, 34, 35, 38, 49, 49, 51, 53, 54, 56, 62, 64, 65, 76, 78, 97, 98, 99]
[4, 5, 12, 13, 15, 17, 18, 23, 25, 27, 34, 34, 35, 38, 49, 49, 51, 53, 54, 56, 62, 64, 65, 76, 78, 97, 98, 99]
[4, 5, 12, 13, 15, 17, 18, 23, 25, 27, 34, 34, 35, 38, 49, 49, 51, 53, 54, 56, 62, 64, 65, 76, 78, 97, 98, 99]
[4, 5, 12, 13, 15, 17, 18, 23, 25, 27, 34, 34, 35, 38, 49, 49, 51, 53, 54, 56, 62, 64, 65, 76, 78, 97, 98, 99]
[4, 5, 12, 13, 15, 17, 18, 23, 25, 27, 34, 34, 35, 38, 49, 49, 51, 53, 54, 56, 62, 64, 65, 76, 78, 97, 98, 99]
[4, 5, 12, 13, 15, 17, 18, 23, 25, 27, 34, 34, 35, 38, 49, 49, 51, 53, 54, 56, 62, 64, 65, 76, 78, 97, 98, 99]
[4, 5, 12, 13, 15, 17, 18, 23, 25, 27, 34, 34, 35, 38, 49, 49, 51, 53, 54, 56, 62, 64, 65, 76, 78, 97, 98, 99]
[4, 5, 12, 13, 15, 17, 18, 23, 25, 27, 34, 34, 35, 38, 49, 49, 51, 53, 54, 56, 62, 64, 65, 76, 78, 97, 98, 99]
[4, 5, 12, 13, 15, 17, 18, 23, 25, 27, 34, 34, 35, 38, 49, 49, 51, 53, 54, 56, 62, 64, 65, 76, 78, 97, 98, 99]
[4, 5, 12, 13, 15, 17, 18, 23, 25, 27, 34, 34, 35, 38, 49, 49, 51, 53, 54, 56, 62, 64, 65, 76, 78, 97, 98, 99]
[4, 5, 12, 13, 15, 17, 18, 23, 25, 27, 34, 34, 35, 38, 49, 49, 51, 53, 54, 56, 62, 64, 65, 76, 78, 97, 98, 99]
[4, 5, 12, 13, 15, 17, 18, 23, 25, 27, 34, 34, 35, 38, 49, 49, 51, 53, 54, 56, 62, 64, 65, 76, 78, 97, 98, 99]
[4, 5, 12, 13, 15, 17, 18, 23, 25, 27, 34, 34, 35, 38, 49, 49, 51, 53, 54, 56, 62, 64, 65, 76, 78, 97, 98, 99]
[4, 5, 12, 13, 15, 17, 18, 23, 25, 27, 34, 34, 35, 38, 49, 49, 51, 53, 54, 56, 62, 64, 65, 76, 78, 97, 98, 99]
[4, 5, 12, 13, 15, 17, 18, 23, 25, 27, 34, 34, 35, 38, 49, 49, 51, 53, 54, 56, 62, 64, 65, 76, 78, 97, 98, 99]
[4, 5, 12, 13, 15, 17, 18, 23, 25, 27, 34, 34, 35, 38, 49, 49, 51, 53, 54, 56, 62, 64, 65, 76, 78, 97, 98, 99]
[4, 5, 12, 13, 15, 17, 18, 23, 25, 27, 34, 34, 35, 38, 49, 49, 51, 53, 54, 56, 62, 64, 65, 76, 78, 97, 98, 99]
[4, 5, 12, 13, 15, 17, 18, 23, 25, 27, 34, 34, 35, 38, 49, 49, 51, 53, 54, 56, 62, 64, 65, 76, 78, 97, 98, 99]
[4, 5, 12, 13, 15, 17, 18, 23, 25, 27, 34, 34, 35, 38, 49, 49, 51, 53, 54, 56, 62, 64, 65, 76, 78, 97, 98, 99]
[4, 5, 12, 13, 15, 17, 18, 23, 25, 27, 34, 34, 35, 38, 49, 49, 51, 53, 54, 56, 62, 64, 65, 76, 78, 97, 98, 99]
Data structure-merge sort