Merge sort (Merge Sort)
The average time complexity: O (n log n)
Space complexity: O (n)
Stability: Stable
The basic idea
An entire array is decomposed into a plurality of areas to a single element in the array is set as a basic unit,
In this case, two reference (left L and right references cited R), using two references, such that each individual element of the array become ordered in each single area.
Finally, the method performing merge (fusion), such that the left and right ordered regions of two single merged, the process is simple recursion.
. 1 public static void mergesort ( int [] ARR, int the begin, int End) { 2 int len = arr.length; . 3 int MID = (the begin End +) / 2 ; . 4 IF (the begin < End) { . 5 // min . 6 mergesort (ARR, the begin, MID); // left ordered . 7 mergesort (ARR,. 1 + MID, End); // the right order 8 @ engagement . 9 Merge (ARR, the begin, MID, End); 10 } 11 } 12 is 13 is Private static void Merge ( int [] ARR, int the begin, int MID, int End) { 14 // temporary array, the array is sorted fragments after storage 15 int [] = TEMP new new int [End - the begin +. 1 ] ; 16 // first element i represents the left side of the array 17 // first element j represents the right array 18 is // K representing the interim array subscripts . 19 int i = the begin, j + = MID. 1, K = 0 ; 20 is 21 is // two sorted arrays into a temporary array 22 is the while (I <= MID && J <=End) { 23 is IF (ARR [I] < ARR [J]) { 24 TEMP [K ++] = ARR [I ++ ]; 25 } the else { 26 is TEMP [K ++] = ARR [J ++ ]; 27 } 28 } 29 // If the left side is drained, the right drained 30 the while (I <= MID) TEMP [K ++] = ARR [I ++ ]; 31 is // if the right is drained, left drained 32 the while (J <= End) TEMP [K ++ ] = ARR [J ++ ]; 33 is for ( int L = 0; L <temp.length; L ++ ) { 34 is arr[begin++] = temp[l]; 35 } 36 }