Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

2079MS 归并排序

Posted by xu_anxi at 2010-04-28 12:06:12 on Problem 2299
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:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator