/** * Created by DELL on 2017/4/23. * 二路归并 */ public class ErLuGuiBInSort { public static void main(String[] args){ int[] in = {46,12,33,72,68,19,80,33}; int[] rein = new int[in.length]; //用于复制保留原纪录序列,辅助空间 int left = 0; //序列in下限 int right = in.length-1; //序列in上限 MergeSort(in,rein,left,right); for(int i=0;i<in.length;i++){ System.out.print(in[i]+" "); } } public static void MergeSort(int[] in,int[] rein,int left,int right){ int middle; if(left<right){ middle = (left+right)/2; MergeSort(in,rein,left,middle); MergeSort(in,rein,middle+1,right); Merge(in,rein,left,right,middle); } } public static void Merge(int[] in,int[] rein,int left,int right,int middle){ int i,p1,p2; for(i=left;i<=right;i++){ // System.out.println("luhan "+in[i]); rein[i] = in[i]; } p1 = left; p2 = middle+1; i = left; while(p1<=middle && p2<=right){ if(rein[p1]<=rein[p2]){ in[i] = rein[p1]; p1++; }else{ in[i] = rein[p2]; p2++; } i++; } while(p1<=middle){ in[i] = rein[p1++]; i++; } while(p2<=right){ // System.out.println("luhandd "+rein[p2]); in[i] = rein[p2++]; i++; } } }
二路归并(JAVA实现)
最新推荐文章于 2024-09-30 13:48:31 发布