写一下归并排序
作者:互联网
public class MergeSortTest { public static void main(String[] args) { int[] arr ={4,7,3,5,6,2,8,1}; mergeSort(arr,0,arr.length-1); System.out.println(Arrays.toString(arr)); } public static void mergeSort(int[] data,int left,int right){ if(left<right){ int mid = (left+right)/2; mergeSort(data,left,mid); mergeSort(data,mid+1,right); merge(data,left,mid,right); } } public static void merge(int[] data,int left,int mid,int right){ int[] temp =new int[data.length]; int loc = left; int point1 = left; int point2 =mid+1; while(point1 <= mid && point2<=right){ if(data[point1] < data[point2]){ temp[loc] = data[point1]; loc++; point1++; }else{ temp[loc] = data[point2]; loc++; point2++; } } while(point1<=mid){ temp[loc++] = data[point1++]; } while(point2<=right){ temp[loc++] = data[point2++]; } for(int i=left;i<=right;i++){ data[i] = temp[i]; } } }
标签:mergeSort,归并,int,一下,void,arr,static,排序,public 来源: https://www.cnblogs.com/Niel-3/p/15648430.html