functioncountInversions($arr){$temp=array_fill(0,count($arr),0);returnmergeSort($arr,$temp,0,count($arr)-1);}functionmergeSort(&$arr,&$temp,$left,$right){$inversions=0;if($left<$right){$mid=(int)(($left+$right)/2);$inversions+=mergeSort($arr,$temp,$left,$mid);$inversions+...
In an array, , the elements at indices and (where ) form an inversion if . In other words, inverted elements and are considered to be "out of order". To correct an inversion, we can swap adjacent elements. For example, consider the dataset . It has two inversions: and . To sort th...