Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
2079MS 归并排序void Merge(long frist,long mid,long last) { long i,k; long begin1,end1,begin2,end2; long *temp=new long[last-frist+1]; begin1=frist; end1=mid; begin2=mid+1; end2=last; k=0; while((begin1<=end1) && (begin2<=end2)) { if(a[begin1]<a[begin2]) { temp[k]=a[begin1]; begin1++; } else { temp[k]=a[begin2]; begin2++; ans=ans+end1-begin1+1; /* 如果a[begin1]>a[begin2],那么说明a[begin1~end1]中所有的数都 大于a[begin2],这样共有end1-begin1+1个数。 */ } k++; } while(begin1<=end1) { temp[k++]=a[begin1++]; } while(begin2<=end2) { temp[k++]=a[begin2++]; } for(int i=0;i<=(last-frist);i++) a[frist+i]=temp[i]; free(temp); } void MergeSort(long frist,long last)//归并排序 { long mid=0; if(frist<last) { mid=(frist+last)/2; MergeSort(frist,mid); MergeSort(mid+1,last); Merge(frist,mid,last); } } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator