归并排序
void merge(int *A,int p,int q,int r){//归并排序 合并数组部分
int *temp1 = new int[q-p+1];
int *temp2 = new int[r-q+1];
for(int i = 0;i<q-p+1;i++){temp1[i] = A[p+i];}
for(int j = 0;j<r-q;j++){temp2[j] = A[q+j+1];}
int flag1 = 0;
int flag2 = 0;
for(int m = 0;m<r-p+1;m++){
if(flag1 == q-p+1&&flag2 == r-q){
return;
}
if(flag1<q-p+1&&flag2 == r-q){
A[m+p] = temp1[flag1];
flag1++;
}else if(flag1 == q-p+1&&flag2<r-q){
A[m+p] = temp2[flag2];
flag2++;
}else if(flag1<q-p+1&&flag2<r-q){
if(temp1[flag1]>temp2[flag2]){
A[m+p] = temp2[flag2];
flag2++;
}else if(temp1[flag1]<=temp2[flag2]){
A[m+p] = temp1[flag1];
flag1++;
}
}
}
delete []temp1;
delete []temp2;
}
void merge_sort(int *A,int p,int r){//归并迭代
if(p<r){
int q = (p+r)/2;
merge_sort(A,p,q);
merge_sort(A,q+1,r);
merge(A,p,q,r);
}
}