# -*- coding: utf-8 -*-

from math import floor, ceil


def sort_and_count_split_inversions (left, right):
    """
        Counts the number of inversions accross left and right.

        An inversion is any pair (i,j), i from left array and j from right
        array, such that i > j.

        Params:
            left: sorted list
            right: sorted list

        Return:
            [{list}, {int}] - sorted array and invertion counter
    """
    n = len(left)
    m = len(right)
    out = []
    i = 0
    j = 0
    count = 0

    while i < n and j < m:
        if left[i] <= right[j]:
            out.append(left[i])
            i += 1
        else:
            out.append(right[j])
            j += 1
            # Count an inversion when an element in the right array
            # is larger the the one in the left array.
            count += n - i
    if i is n:
        while j < m:
            out.append(right[j])
            j += 1
    if j is m:
        while i < n:
            out.append(left[i])
            i += 1
    return [out, count]


def sort_and_count_inversions (arr):
    """
        Divide an conquer routine that recursively splits
        the input array in two halves counting the inversions them summing
        them up.

        Params:
            arr: list of items to count inversions in.

        Return:
            A tuple with (sorted_arr, num_inversions)
    """
    n = len(arr)
    # Base case.
    if n is 1:
        return [arr, 0]
    else:
        # Split the input array in two in the middle.
        left = arr[0:int(floor(n/2))]
        right = arr[int(ceil(n/2)):]

        # Count inversions in each of the smaller arrays.
        [left_sorted, left_count] = sort_and_count_inversions(left)
        [right_sorted, right_count] = sort_and_count_inversions(right)

        # Count inversions only across the arrays.
        [arr_sorted, split_count] = sort_and_count_split_inversions(
                                            left_sorted, right_sorted)

        # Sum up all inversions.
        count = left_count + right_count + split_count

        return [arr_sorted, count]

@@ -859,16 +859,17 @@
 = n - i%0A
+%0A
     if i
@@ -873,39 +873,14 @@
 f i 
-is
+==
  n:%0A
-        while j %3C m:%0A    
     
@@ -879,35 +879,35 @@
  n:%0A        out.
-app
+ext
 end(right%5Bj%5D)%0A  
@@ -905,70 +905,29 @@
 ht%5Bj
+:
 %5D)%0A    
-        j += 1%0A    
+el
 if j 
-is
+==
  m:%0A
-        while i %3C n:%0A    
     
@@ -926,35 +926,35 @@
  m:%0A        out.
-app
+ext
 end(left%5Bi%5D)%0A   
@@ -947,37 +947,20 @@
 d(left%5Bi
+:
 %5D)%0A
-            i += 1
 %0A    ret
@@ -1341,16 +1341,17 @@
 en(arr)%0A
+%0A
     # Ba
@@ -1398,30 +1398,17 @@
 arr, 0%5D%0A
-    else:%0A    
+%0A
     # Sp
@@ -1449,20 +1449,16 @@
 middle.%0A
-    
     left
@@ -1470,33 +1470,17 @@
 r%5B0:
-int(floor(
 n/2
-))
 %5D%0A    
- 
-   
 righ
@@ -1491,30 +1491,15 @@
 arr%5B
-int(ceil(
 n/2
-))
 :%5D%0A%0A
-    
     
@@ -1552,20 +1552,16 @@
 ys.%0A    
-  
-  
 %5Bleft_so
@@ -1616,20 +1616,16 @@
 ft)%0A    
-   
- 
 %5Bright_s
@@ -1680,20 +1680,16 @@
 right)%0A%0A
-    
     # Co
@@ -1727,20 +1727,16 @@
 arrays.%0A
-    
     %5Barr
@@ -1828,28 +1828,24 @@
             
-    
 left_sorted,
@@ -1864,20 +1864,16 @@
 d)%0A%0A    
-  
-  
 # Sum up
@@ -1889,20 +1889,16 @@
 rsions.%0A
-    
     coun
@@ -1941,20 +1941,16 @@
 _count%0A%0A
-    
     retu

fix count inversions for large datasets