Merge Sort: Counting Inversions

  • + 0 comments

    Python3 My Solution

    N_swaps = 0
    def merge_sort(arr):
        n = len(arr)
        if n == 1:
            return arr
        med = n//2
        sub1 = merge_sort(arr[:med])
        sub2 = merge_sort(arr[med:])
    
        global N_swaps
    
        sorted_arr = []
        i, j = 0, 0
        while i < len(sub1) and j < len(sub2):
            if sub1[i] <= sub2[j]:
                sorted_arr.append(sub1[i])
                i += 1
            elif sub1[i] > sub2[j]:
                sorted_arr.append(sub2[j])
                j += 1
                
                N_swaps += len(sub1) - i
        if i < len(sub1):
            sorted_arr += sub1[i:]
            
        if j < len(sub2):
            sorted_arr += sub2[j:]
        return sorted_arr
    
    def countInversions(arr):
        # Write your code here
        global N_swaps
        N_swaps = 0
        merge_sort(arr)
        return N_swaps