For example I have the array:
int IDs[]={1,21,5,3,12,23,2};
The number of pairs that are in the wrong order is 9
. The pairs are: (21, 5) (21,3) (21, 12) (21,2) (5,3) (5,2) (3,2) (12,2) (23,2)
.
So , my algorithm for this implies two for's:
for(int i=0;i<IDs.length;i++)
{
for(int j=i+1;j<IDs.length;j++)
{
if(IDs[i]>IDs[j])
wrong++;
}
}
The problem is that this has a complexity of n2 and I should have a complexity of maximum n*log n.