package com.hanzheng.algorithm;
public class MergeSort {
public static void main(String[] args) {
int [] arr = {91,42,5,6,71,28,4,5,83,7,0};
Utils.print(arr,"排序前:");
process(arr,0,arr.length-1);
Utils.print(arr,"排序后:");
}
private static void process(int[] arr,int L, int R) {
if(L==R){
return;
}
int m = L+((R-L)>>1);
process(arr,L,m);
process(arr, m+1,R);
merge(arr, L, R, m);
}
private static void merge(int[] arr, int L, int R, int m) {
int i = 0;
int p1 = L;
int p2 = m+1;
int help[] = new int[R-L+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 (int j = 0; j < help.length; j++) {
arr[L+j]=help[j];
}
}
}
数组的归并排序
于 2022-07-08 00:07:09 首次发布