Skip to content

Commit e311b02

Browse files
mhammadsaanipoyea
andauthored
Remove unnecessary branch (#4824)
* Algorithm Optimized * Update divide_and_conquer/inversions.py Co-authored-by: John Law <[email protected]> * Update divide_and_conquer/inversions.py Co-authored-by: John Law <[email protected]> * Update divide_and_conquer/inversions.py Co-authored-by: John Law <[email protected]> Co-authored-by: John Law <[email protected]>
1 parent fadb976 commit e311b02

File tree

1 file changed

+10
-26
lines changed

1 file changed

+10
-26
lines changed

Diff for: divide_and_conquer/inversions.py

+10-26
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,25 @@
22
Given an array-like data structure A[1..n], how many pairs
33
(i, j) for all 1 <= i < j <= n such that A[i] > A[j]? These pairs are
44
called inversions. Counting the number of such inversions in an array-like
5-
object is the important. Among other things, counting inversions can help
6-
us determine how close a given array is to being sorted
7-
5+
object is the important. Among other things, counting inversions can help
6+
us determine how close a given array is to being sorted.
87
In this implementation, I provide two algorithms, a divide-and-conquer
98
algorithm which runs in nlogn and the brute-force n^2 algorithm.
10-
119
"""
1210

1311

1412
def count_inversions_bf(arr):
1513
"""
1614
Counts the number of inversions using a a naive brute-force algorithm
17-
1815
Parameters
1916
----------
2017
arr: arr: array-like, the list containing the items for which the number
2118
of inversions is desired. The elements of `arr` must be comparable.
22-
2319
Returns
2420
-------
2521
num_inversions: The total number of inversions in `arr`
26-
2722
Examples
2823
---------
29-
3024
>>> count_inversions_bf([1, 4, 2, 4, 1])
3125
4
3226
>>> count_inversions_bf([1, 1, 2, 4, 4])
@@ -49,20 +43,16 @@ def count_inversions_bf(arr):
4943
def count_inversions_recursive(arr):
5044
"""
5145
Counts the number of inversions using a divide-and-conquer algorithm
52-
5346
Parameters
5447
-----------
5548
arr: array-like, the list containing the items for which the number
5649
of inversions is desired. The elements of `arr` must be comparable.
57-
5850
Returns
5951
-------
6052
C: a sorted copy of `arr`.
6153
num_inversions: int, the total number of inversions in 'arr'
62-
6354
Examples
6455
--------
65-
6656
>>> count_inversions_recursive([1, 4, 2, 4, 1])
6757
([1, 1, 2, 4, 4], 4)
6858
>>> count_inversions_recursive([1, 1, 2, 4, 4])
@@ -72,40 +62,34 @@ def count_inversions_recursive(arr):
7262
"""
7363
if len(arr) <= 1:
7464
return arr, 0
75-
else:
76-
mid = len(arr) // 2
77-
P = arr[0:mid]
78-
Q = arr[mid:]
65+
mid = len(arr) // 2
66+
P = arr[0:mid]
67+
Q = arr[mid:]
7968

80-
A, inversion_p = count_inversions_recursive(P)
81-
B, inversions_q = count_inversions_recursive(Q)
82-
C, cross_inversions = _count_cross_inversions(A, B)
69+
A, inversion_p = count_inversions_recursive(P)
70+
B, inversions_q = count_inversions_recursive(Q)
71+
C, cross_inversions = _count_cross_inversions(A, B)
8372

84-
num_inversions = inversion_p + inversions_q + cross_inversions
85-
return C, num_inversions
73+
num_inversions = inversion_p + inversions_q + cross_inversions
74+
return C, num_inversions
8675

8776

8877
def _count_cross_inversions(P, Q):
8978
"""
9079
Counts the inversions across two sorted arrays.
9180
And combine the two arrays into one sorted array
92-
9381
For all 1<= i<=len(P) and for all 1 <= j <= len(Q),
9482
if P[i] > Q[j], then (i, j) is a cross inversion
95-
9683
Parameters
9784
----------
9885
P: array-like, sorted in non-decreasing order
9986
Q: array-like, sorted in non-decreasing order
100-
10187
Returns
10288
------
10389
R: array-like, a sorted array of the elements of `P` and `Q`
10490
num_inversion: int, the number of inversions across `P` and `Q`
105-
10691
Examples
10792
--------
108-
10993
>>> _count_cross_inversions([1, 2, 3], [0, 2, 5])
11094
([0, 1, 2, 2, 3, 5], 4)
11195
>>> _count_cross_inversions([1, 2, 3], [3, 4, 5])

0 commit comments

Comments
 (0)