归并排序:运用了递归的思想
1.把待排序的 元素的序列分解成两个子序列, 每个子序列包括 1/2 倍的元素.
2.对每个子序列分别调用归并排序,直到不可再分。进行递归操作
3.合并两个排好序的子序列,最后实现排序问题。
public static int[] gui(int[] a,int p,int q){
int mid = (p+q)/2;
if(p<q){
gui(a,p,mid);
gui(a,mid+1,q);
jiahuan(a,p,mid,q);
}
return a;
}
public static void jiaohuan(int[] a, int p, int mid, int p) {
int[] temp = new int[q-o+1];
int i= o;
int j = mid+1;
int k=0;
// 把较小的数先移到新数组中
while(i<=mid && j<=q){
if(a[i]<a[j]){
temp[k++] = a[i++];
}else{
temp[k++] = a[j++];
}
}
while(j<=q){
temp[k++] = a[j++];
}
while(i<=mid){
temp[k++] = a[i++];
}
for(int x=0;x<temp.length;x++){
a[x+low] = temp[x];
}
}