mtjmtj7的小站
mtjmtj7的小站
© mtjmtj7
All Rights Reserved.

JAVA-归并排序


public class MergeSort { public static int[] sort(int[] a,int low,int high){ int mid = (low+high)/2; if(low<high){ sort(a,low,mid); sort(a,mid+1,high); //左右归并 merge(a,low,mid,high); } return a; } public static void merge(int[] a, int low, int mid, int high) { int[] temp = new int[high-low+1]; int i= low; int j = mid+1; int k=0; // 把较小的数先移到新数组中 while(i<=mid && j<=high){ if(a[i]<a[j]){ temp[k++] = a[i++]; }else{ temp[k++] = a[j++]; } } // 把左边剩余的数移入数组 while(i<=mid){ temp[k++] = a[i++]; } // 把右边边剩余的数移入数组 while(j<=high){ temp[k++] = a[j++]; } // 覆盖原数组 for(int x=0;x<temp.length;x++){ a[x+low] = temp[x]; } } public static void main(String[] args) { int a[] = {1,7,3,8,4,2,8,0,5,3}; sort(a, 0, a.length-1); for (int i = 0; i < a.length; i++) { System.out.print(a[i] + " "); } } }
打赏
2019-03-10
15 阅读
暂无评论

发表评论