-
Notifications
You must be signed in to change notification settings - Fork 1
/
count_number_of_inversions.py
68 lines (54 loc) · 2.34 KB
/
count_number_of_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
62
63
64
65
"""
This is for an educational purposes
This is a good practice for intermediate python learners.
The definition of the problem: Suppose you are given a list of n elements with an arbitrary order and
you are asked to find the number of inversions in a given list by using Divide & Conquer Paradigm in computer science
Here is the definition of inversion: Number of pairs(i, j) of list indices i < j and List[i] > List[j]
for example it is given (2,3,5,6,1) the inversions are: (2,1) and (6,1)
Please note that you need a pen a piece of paper first to understand the problem and algorithm which also is explained
in https://class.coursera.org/algo-004/lecture/16?s=e
I highly recommend first to understand your algorithm in the paper and then get into the code!
"""
def sort_and_count(iterable):
"""
This function recursively calls itself to break up the list into two parts and then recursively recover them
the actual algorithm is implemented in this function
"""
if len(iterable) == 1:
return iterable, 0
else:
(sorted_left, left_inversion_number) = sort_and_count(iterable[:(len(iterable) / 2)])
(sorted_right, right_inversion_number) = sort_and_count(iterable[len(iterable) / 2:])
(result, split_inversion_number) = merge_and_count(sorted_left,sorted_right)
return result,left_inversion_number + right_inversion_number + split_inversion_number
def find_inversions(iterable):
"""
Since sort_and_count function returns a tuple and I just need the number of inversions first part is enough for
me
List:parameter
Number:return
"""
return sort_and_count(iterable)[1]
def merge_and_count(first, second):
"""
It is explained during lecture that while implementing merge part there is a single path that
counts the inversions automatically if the second part of the
"""
i = 0
j = 0
ret = []
number_of_inversion = 0
while len(ret) != len(first) + len(second):
if i == len(first):
ret += second[j:]
elif j == len(second):
ret += first[i:]
else:
if first[i] < second[j]:
ret.append(first[i])
i += 1
else:
ret.append(second[j])
j += 1
number_of_inversion += len(first) - i
return ret,number_of_inversion