归并排序(递归实现)
public static void mergeSort1(int[] arr){ if(arr ==null || arr.length <2){ return; } process(arr, 0 , arr.length-1); } public static void process(int[] arr ,int L ,int R){ if(L ==R){ return; } int mid = L +((R-L)>>1); process(arr , L , mid); process(arr , mid+1 , R); merge(arr , L , mid ,R); } public static void merge(int[] arr , int L , int M , int R){ int[] help = new int[R-L+1]; int i =0; int p1 = L; int p2 = M+1; while(p1 <= M && p2 <= R){ help[i++] = arr[p1] <= arr[p2] ? arr[p1++] :arr[p2++]; } while (p1 <= M){ help[i++] = arr[p1++];; } while (p2 <= R){ help[i++] = arr[p2++];; } for (i = 0; i < help.length; i++) { arr[L+i] = help[i]; } }
原文地址:https://blog.csdn.net/2401_83010439/article/details/140372494
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!