I. Basic Ideas
Merge Sorting is an effective sort based on the merge operation.Algorithm.
This algorithm is a very typical application of divide and conquer.
Divides an array into smaller sub-lists. Each sub-list is sorted separately, and then merged to form a larger ordered list.
Merging sub-lists by merging sub-list elements means merging and sorting (merge sort)
Ii. Algorithm Description
1. Split a list into two sublists
2. The index [first, mid] is called for the 1st list to define the lower half of the original sequence;
3. The index [mid, last] is called for the 2nd list to define the upper part of the original sequence;
4. This process generates a recursive call chain to separate the original list into smaller child lists (half list) until these child lists are 1 in length (stop condition)
5. merge these sublists in reverse order to form a larger sublist;
6. The final merge of the sub-list corresponds to the first recursive step, and the original array is arranged in an ordered state.
Iii. Example Code
Public class merge {public static void mergesort (INT [] ARR) {// create a temporary array to store partitioned elements int [] temparr = (INT []) Arr. clone (); // call msort with arrays arr and temparr along with the index range msort (ARR, temparr, 0, arr. length);} Private Static void msort (INT [] arr, int [] temparr, int first, int last) {// If the sublist has more than 1 element, continue if (first + 1 <last) {// If the sublist of size 2 or more, call msort () // For the Left and Right sublists and then merge the sorted sublists using Merge () int midpt = (first + last)/2; msort (ARR, temparr, first, midpt ); msort (ARR, temparr, midpt, last); // If list is already sorted, just copy from SRC to DEST; // This is an optimization that results in faster sorts for nearly ordered list if (icomparable) Arr [midpt-1]). compareto (ARR [midpt]) <0) {return;} // The element in the ranges [first, mid] and [mid, last] are ordered; // merge the ordered sublists into an ordered sequence in the range [first, last] // using the temporary array int Indexa, indexb, indexc; // set Indexa to scan sublist A with range [first, mid] // and indexb to scan sublist B with range [mid, last] Indexa = first; indexb = midpt; indexc = first; // while both sublists are not exhausted, compare arr [Indexa] and ARR [indexb]; // copy the smaller to temparr while (Indexa <midpt & indexb <last) {If (icomparable) Arr [Indexa]). compareto (ARR [indexb]) <0) {temparr [indexc] = arr [Indexa]; // copy to temparr Indexa ++ ;} else {temparr [indexc] = arr [indexb]; // copy to temparr indexb ++;} // increment indexc ++ ;} // copy the tail of the sublist that is not exhausted while (Indexa <midpt) {temparr [indexc] = arr [Indexa]; // copy to temparr Indexa ++; indexc ++;} while (indexb <last) {temparr [indexc] = arr [indexb]; // copy to temparr indexb ++; indexc ++ ;} // copy the elements from temporary array to original array for (INT I = first; I <last; I ++) {arr [I] = temparr [I] ;}}}
Iv. Efficiency Analysis
Stability
Space complexity: O (N)
Time Complexity: O (nlog2n)
Worst case: O (nlog2n)
Best case: O (nlog2n)