归并排序
作者:互联网
时间复杂度:O(nlogn)
void merge_sort(int l,int r)
{
if(l>=r) return;
int mid=l+r>>1;
merge_sort(l,mid);
merge_sort(mid+1,r);
int p=l,q=mid+1,k=l;
while(p<=mid&&q<=r){
if(a[p]<=a[q]) t[k++]=a[p++];
else{
t[k++]=a[q++];
ans+=mid-p+1;
}
}
while(p<=mid) t[k++]=a[p++];
while(q<=r) t[k++]=a[q++];
for(int i=l;i<=r;++i) a[i]=t[i];
}
标签:sort,归并,nlogn,return,int,mid,merge,排序 来源: https://www.cnblogs.com/unravel-CAT/p/15355948.html