int Inversepairs (int* data, int length)
{
if (data = = NULL | | length < 0)
return 0;
int *copy = new Int[length];
for (int i = 0; i < length; ++i)
Copy[i] = Data[i];
int count = Inversepairscore (data, copy, 0, length-1);
delete[] copy;
return count;
}
int Inversepairscore (int* data, int* copy, int start, int end)
{
if (start = = end)
{
Copy[start] = Data[start];
return 0;
}
int length = (end-start)/2;
int left = Inversepairscore (copy, data, start, start + length);
int right = Inversepairscore (copy, data, start + length + 1, end);
I initialize the subscript of the last digit in the first half of the paragraph
int i = start + length;
J initializes the subscript for the last digit in the second half of the paragraph
int j = END;
int indexcopy = end;
int count = 0;
while (i >= start&& J >= Start + length + 1)
{
if (Data[i] > Data[j])
{
copy[indexcopy--] = data[i--];
Count + = J-start-length;
}
Else
{
copy[indexcopy--] = data[j--];
}
}
for (; I >= start; i.)
copy[indexcopy--] = Data[i];
for (; J >= Start + length + 1;--j)
copy[indexcopy--] = Data[j];
Return left + right + count;
}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
36. The reverse order in the array