*注意命名规范,写程序,边界条件,、
递归怎么写,假如为空的时候(或者只有一个的时候);然后做完左边,然后做完右边,然后处理左右边的结果。
1.逆序对,int a[]
int findTheReverse(int a[],int low ,int high)
{
int copy[]=new int [strlen(a)];
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;
}
递归怎么写,假如为空的时候(或者只有一个的时候);然后做完左边,然后做完右边,然后处理左右边的结果。
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;
}