forked from keshavsingh4522/hacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hacktober
61 lines (44 loc) · 1.19 KB
/
hacktober
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
def inversionsCount(arr, n):
tmp_arr = [0]*n
return mergeSort(arr, tmp_arr, 0, n-1)
def mergeSort(arr, temp_arr, left, right):
inv_count = 0
if left < right:
mid = (left + right)//2
inv_count += mergeSort(arr, temp_arr, left, mid)
inv_count += mergeSort(arr, temp_arr, mid+1, right)
inv_count += merge(arr, temp_arr, left, mid, right)
return inv_count
def merge(arr, temp_arr, left, mid, right):
i = left
j = mid+1
k = left
inv_count = 0
while i <= mid and j <= right:
if arr[i] <= arr[j]:
temp_arr[k] = arr[i]
k += 1
i += 1
else:
# Inversion will occur
temp_arr[k] = arr[j]
inv_count += (mid-i + 1)
k += 1
j += 1
while i <= mid:
temp_arr[k] = arr[i]
k += 1
i += 1
while j <= right:
temp_arr[k] = arr[j]
k += 1
j += 1
for var in range(left, right + 1):
arr[var] = temp_arr[var]
print(arr)
return inv_count
# Driver's Code
arr = [1, 20, 6, 4, 5, 19]
n = len(arr)
result = inversionsCount(arr, n)
print("Number of inversions are", result)