Merge sort is used to divide and conquer thought, namely divide-cure-three steps, the average time complexity of the algorithm is O (NLGN).
(i) Algorithm implementation
1 Private voidMerge_sort (int[] Array,intFirstintLast ) {2 if(First + 1 <Last ) {3 intMid = (first + last)/2;4 Merge_sort (Array, first, mid);5 Merge_sort (Array, Mid, last);6 7 //Merge8 inti =First ;9 intj =mid;Ten int[] Temparray =New int[Last-First ]; One for(intk = 0; K < Temparray.length; k++) { A if(J = =Last ) { -Temparray[k] = array[i++]; -}Else if(i = =mid) { theTemparray[k] = array[j++]; -}Else if(Array[i] <Array[j]) { -Temparray[k] = array[i++]; -}Else if(Array[i] >=Array[j]) { +Temparray[k] = array[j++]; -}Else { + Throw NewRuntimeException ("Merge sort error.")); A } at } -System.arraycopy (temparray, 0, array, first, last-First ); - } -}
Merge Sort
1) The algorithm is not sorted in situ and requires additional space when merging
2) The average time complexity of the algorithm is O (NLGN)
(ii) Simulation results
**************************************************
Number to Sort is:2500
Array to sort is:{633649,71377,977368,56277,635475,755791,746668,373470,820128,669757 ...}
Cost time of "Mergsort" is (milliseconds): 1
Sort result of "Mergsort": {695,711,935,1074,1571,1681,2434,3967,4104,4452 ...}
**************************************************
Number to Sort is:25000
Array to sort is:{62806,677129,876202,279782,662046,978299,979879,374196,303999,484652 ...}
Cost time of "Mergsort" is (milliseconds): 5
Sort result of "Mergsort": {15,34,74,94,131,141,316,372,375,417 ...}
**************************************************
Number to Sort is:250000
Array to sort is:{617507,750026,202102,267444,690991,638097,868240,413838,774218,463780 ...}
Cost time of "Mergsort" is (milliseconds): 67
Sort result of "Mergsort": {6,9,19,28,31,39,44,45,56,59 ...}
Related code:
1 PackageCom.cnblogs.riyueshiwang.sort;2 3 Importjava.util.Arrays;4 5 Public classMergeSortextendsAbstractsort {6 7 @Override8 protected voidSortint[] tosort) {9Merge_sort (tosort, 0, tosort.length);Ten } One A Private voidMerge_sort (int[] Array,intFirstintLast ) { - if(First + 1 <Last ) { - intMid = (first + last)/2; the Merge_sort (Array, first, mid); - Merge_sort (Array, Mid, last); - - //Merge + inti =First ; - intj =mid; + int[] Temparray =New int[Last-First ]; A for(intk = 0; K < Temparray.length; k++) { at if(J = =Last ) { -Temparray[k] = array[i++]; -}Else if(i = =mid) { -Temparray[k] = array[j++]; -}Else if(Array[i] <Array[j]) { -Temparray[k] = array[i++]; in}Else if(Array[i] >=Array[j]) { -Temparray[k] = array[j++]; to}Else { + Throw NewRuntimeException ("Merge sort error.")); - } the } *System.arraycopy (temparray, 0, array, first, last-First ); $ }Panax Notoginseng } - the Public Static voidMain (string[] args) { + for(intj = 0, n = 2500; J < 3; j + +, n = n * 10) { A System.out the. println ("**************************************************"); +System.out.println ("Number to Sort is:" +n); - int[] array = Commonutils.getrandomintarray (n, 1000000); $System.out.print ("Array to sort is:"); $ Commonutils.printintarray (array); - - int[] Array1 =arrays.copyof (array, n); the Newmergesort (). Sortandprint (array1); - }Wuyi } the}
Mergesort.java
Sort algorithm two: merge sort