I recently looked at the source code under sort 1.7 and found that the Meger sort optimized--timsort was used when the ordering element was less than 32. In the spirit of tracing back to the source, I first went to see the next merge sort in Java implementation, although the principle of merging sorting has been clear, but never wrote. On the internet to find someone else to implement the good code, and then fill in their own comments, recorded as follows for future review (the code's tail recursive theory can be converted to iterations to reduce space use, prevent stack overflow)
First, there is a basic merge algorithm that implements the merging of two ordered arrays.
1 /**2 * Merge two ordered arrays into one (increment) here slightly trickery, two ordered arrays are a[beginfrist,beginsecond respectively and [Beginsecond,endsecond]3 * @parama specifies the array a4 * @parambeginfrist The start position of the first ordered array5 * @paramBeginsecond the end position of the first ordered array (the start position of the second ordered array)6 * @paramEndsecond The end of a second ordered array7 */8 Private Static voidMergeint[] A,intBeginfrist,intBeginsecond,intEndsecond) {9 //Create an intermediate array to hold the ordered array data after merging the two ordered arraysTen int[] tmp =New int[Endsecond-beginfrist + 1]; One //Initialize each of the intermediate variables A intTempfristlocation = beginfrist, tempsecondlocation= beginsecond, templocation = 0; - //iterate through two arrays and sort them until an array is fully traversed. - while(Tempfristlocation < Beginsecond && tempsecondlocation <=Endsecond) { the if(A[tempfristlocation] <=A[tempsecondlocation]) { -Tmp[templocation] =A[tempfristlocation]; -tempfristlocation++; -}Else { +Tmp[templocation] =A[tempsecondlocation]; -tempsecondlocation++; + } Atemplocation++; at } - //Add the remaining portion of the array that is fully traversed to the last, because the two arrays themselves are ordered, so the remainder must be larger than the existing ones. - while(Tempfristlocation <Beginsecond) { -Tmp[templocation] =A[tempfristlocation]; -tempfristlocation++; -templocation++; in } - while(Tempsecondlocation <=Endsecond) { toTmp[templocation] =A[tempsecondlocation]; +tempsecondlocation++; -templocation++; the } * //Copy temp to given array a $System.arraycopy (TMP, 0, A, beginfrist, tmp.length);Panax Notoginseng}
The merge main algorithm is as follows:
1 /**2 * Merge the main algorithm, where tail recursion is used and can theoretically be converted to an iteration3 * @parama ordered array4 * @paramlen number of elements in each heap5 */6 Public Static voidMergeSort (int[] A,intLen) {7 //Initializing Data8 intSize =a.length,s;9 //take the middle position, here << 's bit operation can be understood asTen intMID = size/(len << 1); One //merging to the end of an ordered set of algorithms A if(Mid = = 0) - return; - //make a trip merge sort the for(inti = 0; I < mid; ++i) { - //S is the start position of the first ordered array -s = i * 2 *Len; - //Call Merge Sort +Merge (A, S, S + len, (len << 1) + s-1); - } + /*here, because Len must be a power of N of 2, the size and ((Len << 1)-1) and operation, as long as not 0 can represent the size is not a multiple of Len, that is, Len can not be divisible by zise A that is, some of the data is not being merged after the mid-time merge.*/ at intc = size & (len << 1)-1); - - //Merge the remaining number and the last ordered set - if(c! = 0) -Merge (A, size-c-2 * len, size-c, size-1); - //-------recursively perform the next merge sort------// inMergeSort (A, 2 *len); -}
The test method is as follows:
1 Public Static voidMain (string[] args) {2Random random =NewRandom ();3 int[] A =New int[8];4 for(inti = 0; i < a.length; i++) {5A[i]=random.nextint (100);6 }7MergeSort (A, 1);8 for(inti = 0; i < a.length; ++i) {9System.out.print (A[i] + "");Ten } One}
Java implementation of merge sort