SAYAN-2000
/
Must-Do-Coding-Questions-For-Service-Based-Companies-and-Product-Based-Companies.
Public
forked from rohitjila/Must-Do-Coding-Questions-For-Service-Based-Companies-and-Product-Based-Companies.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCount Inversion
42 lines (38 loc) · 1.08 KB
/
Count Inversion
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
class Solution:
def inversionCount(self, arr, n):
temp=[0]*n
return self._mergesort(arr,temp,0,n-1)
def _mergesort(self,arr,temp,left,right):
ic=0
if (left < right):
mid=(left+(right-left)//2)
ic+=self._mergesort(arr,temp,left,mid)
ic+=self._mergesort(arr,temp,mid+1,right)
ic+=self.merge(arr,temp,left,mid,right)
return ic
def merge(self,arr,temp,left,mid,right):
i=left
j=mid+1
k=left
ic=0
while(i <= mid and j <= right):
if (arr[i] <= arr[j]):
temp[k]=arr[i]
k+=1
i+=1
else:
temp[k]=arr[j]
ic+=(mid-i+1)
j+=1
k+=1
while(i <= mid):
temp[k]=arr[i]
i+=1
k+=1
while(j <= right):
temp[k]=arr[j]
j+=1
k+=1
for m in range(left,right+1):
arr[m]=temp[m]
return ic