-
Notifications
You must be signed in to change notification settings - Fork 0
/
Inversions.py
61 lines (43 loc) · 1.71 KB
/
Inversions.py
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
# Inversion:
# if i < j then A[i] > A[j] would be an inversion for any pair of (i,j)
count = 0
def inver_count(input_list):
global count
if (len(input_list) == 1):
return input_list
# Recursion and count
else:
first_half = inver_count(input_list[0:int(len(input_list)/2)])
second_half = inver_count(input_list[int(len(input_list)/2):])
return merge_and_count(first_half, second_half)
def merge_and_count(first_array, second_array):
global count
merged_array = []
i = 0
j = 0
for k in range(0, len(first_array) + len(second_array)):
if (i < len(first_array) and j < len(second_array)):
if (first_array[i] < second_array[j]):
merged_array.append(first_array[i])
i += 1
elif (second_array[j] < first_array[i]):
merged_array.append(second_array[j])
j += 1
count = count + (len(first_array) - i)
elif (first_array[i] == second_array[j]):
merged_array.append(first_array[i])
merged_array.append(second_array[j])
i += 1
j += 1
# Account for running out of index in each sub array | Or running out of index in both
elif (i >= len(first_array) and j >= len(second_array)):
return merged_array
elif (i >= len(first_array)):
merged_array.append(second_array[j])
j += 1
elif (j >= len(second_array)):
merged_array.append(first_array[i])
i += 1
return merged_array
print(inver_count([12,11,13,5,6,7]))
print(count)