实现:
二路归并
public class TestMergeSort { public int[] mergeSortArray(int[] arr, int left, int right){ if(left >= right) return null; int mid = (left+right)/2; mergeSortArray(arr, left, mid); mergeSortArray(arr, mid+1, right); merge(arr, left, mid, right); return arr; } public int[] merge(int[] arr, int left, int mid, int right){ int[] temp = new int[arr.length]; int r1 = mid +1; int tIndex = left; int cIndex = left; while(left<=mid && r1<=right){ if(arr[left] <= arr[r1]){ temp[tIndex++] = arr[left++]; }else{ temp[tIndex++] = arr[r1++]; } } while(left <= mid){ temp[tIndex++] = arr[left++]; } while(r1<=right){ temp[tIndex++] = arr[r1++]; } while(cIndex <= right){ arr[cIndex] = temp[cIndex]; cIndex++; } return arr; } public static void main(String[] args) { int[] arr = {6,2,4,1,5,9}; TestMergeSort test = new TestMergeSort(); test.mergeSortArray(arr, 0, arr.length -1); for(int i = 0 ; i < arr.length; i ++){ System.out.println(arr[i]); } } }
这个哥哥讲的不错,跟他学习了:
http://www.cnblogs.com/shudonghe/archive/2013/09/05/3302888.html
这个可以看看:
http://www.cnblogs.com/luweiseu/archive/2012/07/14/2591653.html