Skip to content

Commit f7ee5fb

Browse files
divide and conquer and brute force algorithms for array-inversions counting
1 parent 2dc1265 commit f7ee5fb

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

Diff for: divide_and_conquer/inversions.py

+31
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,17 @@ def count_inversions_bf(arr):
2626
-------
2727
num_inversions: The total number of inversions in `arr`
2828
29+
Examples
30+
---------
31+
32+
>>> count_inversions_bf([1, 4, 2, 4, 1])
33+
3
34+
35+
>>> count_inversions_bf([1, 1, 2, 4, 4]
36+
0
37+
38+
>>> count_inversions_bf([])
39+
0
2940
"""
3041

3142
num_inversions = 0
@@ -53,6 +64,17 @@ def count_inversions_recursive(arr):
5364
C: a sorted copy of `arr`.
5465
num_inversions: int, the total number of inversions in 'arr'
5566
67+
Examples
68+
--------
69+
70+
>>> count_inversions_recursive([1, 4, 2, 4, 1])
71+
[1, 1, 2, 4, 4], 3
72+
73+
>>> count_inversions_recursive([1, 1, 2, 4, 4])
74+
[1, 1, 2, 4, 4], 0
75+
76+
>>>> count_inversions_recursive([])
77+
[], 0
5678
"""
5779
if len(arr) <= 1:
5880
return arr, 0
@@ -87,6 +109,15 @@ def _count_cross_inversions(P, Q):
87109
R: array-like, a sorted array of the elements of `P` and `Q`
88110
num_inversion: int, the number of inversions across `P` and `Q`
89111
112+
Examples
113+
--------
114+
115+
>>> _count_cross_inversions([1, 2, 3], [0, 2, 5])
116+
[0, 1, 2, 2, 3, 5], 4
117+
118+
>>> _count_cross_inversions([1, 2, 3], [3, 4, 5])
119+
[1, 2, 3, 3, 4, 5], 0
120+
90121
"""
91122

92123
R = []

0 commit comments

Comments
 (0)