-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmerge_sort_count_inversions.py
More file actions
59 lines (43 loc) · 1.36 KB
/
Copy pathmerge_sort_count_inversions.py
File metadata and controls
59 lines (43 loc) · 1.36 KB
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
def count_inversions(a):
temp = a[:] # [None for x in range(len(a))]
return mergesort(a, temp, 0, len(a)-1)
def mergesort(a, temp, leftStart, rightEnd):
if (leftStart >= rightEnd):
return 0
mid = (leftStart + rightEnd) / 2
inversions = 0
inversions += mergesort(temp, a, leftStart, mid)
inversions += mergesort(temp, a, mid+1, rightEnd)
inversions += mergeHalves(a, temp, leftStart, rightEnd)
# print a
return inversions
def mergeHalves(a, temp, leftStart, rightEnd):
mid = (leftStart + rightEnd) / 2
sz = rightEnd - leftStart + 1
leftEnd = mid
rightStart = mid + 1
left = leftStart
right = rightStart
index = leftStart
inversions = 0
while left <= leftEnd or right <= rightEnd:
if left > leftEnd:
a[index] = temp[right]
right += 1
elif right > rightEnd:
a[index] = temp[left]
left += 1
elif temp[left] <= temp[right]:
a[index] = temp[left]
left += 1
else: # if a[left] > a[right]:
a[index] = temp[right]
right += 1
inversions += mid + 1 - left
index += 1
return inversions
t = int(raw_input().strip())
for a0 in xrange(t):
n = int(raw_input().strip())
arr = map(int, raw_input().strip().split(' '))
print count_inversions(arr)