逆序数的个数利用归并排序递归解决

来源:互联网 发布:2016免费翻墙软件 编辑:程序博客网 时间:2024/06/07 14:36
*注意命名规范,写程序,边界条件,、


递归怎么写,假如为空的时候(或者只有一个的时候);然后做完左边,然后做完右边,然后处理左右边的结果。


1.逆序对,int  a[]
int  findTheReverse(int a[],int low ,int high)
{
   int copy[]=new int [strlen(a)];

   return findTheReverseCope(a,copy,low,high);

  delete copy[];

}


int  findTheReverseCope(int *a,int *copy,int low,int high)
{
   if(low==high) {copy[low]=a[low];return  0;}
   int length=(high-low)/2;
   int left=findTheReverseCope(a,copy,low,low+length);
   int right=findTheReverseCope(a,copy,lenght+low+1,high);
   int i=low+length,j=high;int sum=0;
   int indexcopy=high;//动脑,对头的。
   while(i>=low  &&  j>=low+length+1)
   {
      if ( a[low+length]>a[high]  )
 {
   sum+=high-low-length;
copy[indexcopy--]=a[i--];
 }
 else
 {
  copy[indexcopy--]=a[j--];
 }
   }
   //完了吗?没,有可能两个序列其中还有还没弄完。
   return sum+left+right;
   
}
0 0