归并排序(Merge sort)用到了分治思想,即分-治-合三步,算法平均时间复杂度是O(nlgn).

(一)算法实现

 1 private void merge_sort(int[] array, int first, int last) {
 2         if (first + 1 < last) {
 3             int mid = (first + last) / 2;
 4             merge_sort(array, first, mid);
 5             merge_sort(array, mid, last);
 6 
 7             // merge
 8             int i = first;
 9             int j = mid;
10             int[] tempArray = new int[last - first];
11             for (int k = 0; k < tempArray.length; k++) {
12                 if (j == last) {
13                     tempArray[k] = array[i++];
14                 } else if (i == mid) {
15                     tempArray[k] = array[j++];
16                 } else if (array[i] <= array[j]) {
17                     tempArray[k] = array[i++];
18                 } else if (array[i] > array[j]) {
19                     tempArray[k] = array[j++];
20                 } else {
21                     throw new RuntimeException("Merge sort error.");
22                 }
23             }
24             System.arraycopy(tempArray, 0, array, first, last - first);
25         }
26     }
Merge sort

 

1)算法不是原地排序,合并时需要额外空间

2)算法平均时间复杂度是O(nlgn)

3)归并排序属于比较排序 

4)归并排序属于稳定排序算法

(二)仿真结果

**************************************************
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...}

相关代码:

 1 package com.cnblogs.riyueshiwang.sort;
 2 
 3 import java.util.Arrays;
 4 
 5 public class MergeSort extends abstractSort {
 6 
 7     @Override
 8     protected void sort(int[] toSort) {
 9         merge_sort(toSort, 0, toSort.length);
10     }
11 
12     private void merge_sort(int[] array, int first, int last) {
13         if (first + 1 < last) {
14             int mid = (first + last) / 2;
15             merge_sort(array, first, mid);
16             merge_sort(array, mid, last);
17 
18             // merge
19             int i = first;
20             int j = mid;
21             int[] tempArray = new int[last - first];
22             for (int k = 0; k < tempArray.length; k++) {
23                 if (j == last) {
24                     tempArray[k] = array[i++];
25                 } else if (i == mid) {
26                     tempArray[k] = array[j++];
27                 } else if (array[i] <= array[j]) {
28                     tempArray[k] = array[i++];
29                 } else if (array[i] > array[j]) {
30                     tempArray[k] = array[j++];
31                 } else {
32                     throw new RuntimeException("Merge sort error.");
33                 }
34             }
35             System.arraycopy(tempArray, 0, array, first, last - first);
36         }
37     }
38 
39     public static void main(String[] args) {
40         for (int j = 0, n = 2500; j < 3; j++, n = n * 10) {
41             System.out
42                     .println("**************************************************");
43             System.out.println("Number to Sort is:" + n);
44             int[] array = CommonUtils.getRandomIntArray(n, 1000000);
45             System.out.print("Array to sort is:");
46             CommonUtils.printIntArray(array);
47 
48             int[] array1 = Arrays.copyOf(array, n);
49             new MergeSort().sortAndprint(array1);
50         }
51     }
52 }
MergeSort.java