归并排序模板
2021-06-06 06:05
标签:ble ret line com while model else expand end code:归并排序模板
#include
#include
#include
#include
using namespace std;
const int N = 1e5 + 10;
typedef long long LL;
LL q[N],tmp[N];
int n;
void merge_sort(LL * q,int l,int r){
//边界判断
if(l>=r) return;
//递归处理子问题
int mid = l+r>>1;
merge_sort(q,l,mid);merge_sort(q,mid+1,r);
//合并
int i=l,j=mid+1,k=0;
while(imid && jr){
if(q[i] q[j]) tmp[k++] = q[i++];
else tmp[k++] = q[j++];
}
while(imid) tmp[k++] = q[i++];
while(jr) tmp[k++] = q[j++];
//将数据拷回原数组
for(i=l,k=0;ir;i++,k++) q[i] = tmp[k];
}
int main(){
scanf("%d",&n);
for(int i=0;in;i++) scanf("%lld",&q[i]);
merge_sort(q,0,n-1);
for(int i=0;in;i++) printf("%lld ",q[i]);
return 0;
}
归并排序模板
标签:ble ret line com while model else expand end
原文地址:https://www.cnblogs.com/yuanshixiao/p/14614580.html
上一篇:python开发基础(三)集合
下一篇:C++ 按行输入