标签:
1 import java.util.Arrays; 2 3 4 public class MergeSort { 5 6 public static void main(String[] args) { 7 MergeSort ms=new MergeSort(); 8 int[] arr={5,44,32,4,566,67,23}; 9 ms.merge(arr, 0, arr.length-1); 10 System.out.println(Arrays.toString(arr)); 11 } 12 13 void merge(int[] arr,int start,int end ) 14 { 15 if(start==end) 16 return ; 17 int mid=(start+end)/2; 18 merge(arr,start,mid);//左边排序递归 19 merge(arr,mid+1,end);//you边排序递归 20 combine(arr,start,mid,end); 21 } 22 23 void combine(int[] arr,int start,int mid,int end) { 24 int length=end-start+1; 25 int tempArr[]=new int[length]; 26 int tempIndex=0; 27 int left=start; 28 int right=mid+1; 29 while(left<=mid&&right<=end) 30 { 31 if(arr[left]<arr[right]) 32 tempArr[tempIndex++]=arr[left++]; 33 else 34 tempArr[tempIndex++]=arr[right++]; 35 } 36 37 while(left<=mid) 38 tempArr[tempIndex++]=arr[left++]; 39 40 while(right<=end) 41 tempArr[tempIndex++]=arr[right++]; 42 43 for(int i=0;i<tempArr.length;i++) 44 { 45 arr[start+i]=tempArr[i]; 46 } 47 } 48 }
标签:
原文地址:http://www.cnblogs.com/sweetculiji/p/4292409.html