From 3885a60be5808b1eb6e74650444ed0be01e5c2a1 Mon Sep 17 00:00:00 2001 From: Muhammad Hammad Sani <58339378+mhammadsaani@users.noreply.github.com> Date: Fri, 1 Oct 2021 13:15:40 +0500 Subject: [PATCH 1/4] Algorithm Optimized --- divide_and_conquer/inversions.py | 33 +++++++++----------------------- 1 file changed, 9 insertions(+), 24 deletions(-) diff --git a/divide_and_conquer/inversions.py b/divide_and_conquer/inversions.py index 9bb656229321..8b43bc011958 100644 --- a/divide_and_conquer/inversions.py +++ b/divide_and_conquer/inversions.py @@ -4,29 +4,23 @@ called inversions. Counting the number of such inversions in an array-like object is the important. Among other things, counting inversions can help us determine how close a given array is to being sorted - In this implementation, I provide two algorithms, a divide-and-conquer algorithm which runs in nlogn and the brute-force n^2 algorithm. - """ def count_inversions_bf(arr): """ Counts the number of inversions using a a naive brute-force algorithm - Parameters ---------- arr: arr: array-like, the list containing the items for which the number of inversions is desired. The elements of `arr` must be comparable. - Returns ------- num_inversions: The total number of inversions in `arr` - Examples --------- - >>> count_inversions_bf([1, 4, 2, 4, 1]) 4 >>> count_inversions_bf([1, 1, 2, 4, 4]) @@ -49,20 +43,16 @@ def count_inversions_bf(arr): def count_inversions_recursive(arr): """ Counts the number of inversions using a divide-and-conquer algorithm - Parameters ----------- arr: array-like, the list containing the items for which the number of inversions is desired. The elements of `arr` must be comparable. - Returns ------- C: a sorted copy of `arr`. num_inversions: int, the total number of inversions in 'arr' - Examples -------- - >>> count_inversions_recursive([1, 4, 2, 4, 1]) ([1, 1, 2, 4, 4], 4) >>> count_inversions_recursive([1, 1, 2, 4, 4]) @@ -72,40 +62,35 @@ def count_inversions_recursive(arr): """ if len(arr) <= 1: return arr, 0 - else: - mid = len(arr) // 2 - P = arr[0:mid] - Q = arr[mid:] + + mid = len(arr) // 2 + P = arr[0:mid] + Q = arr[mid:] - A, inversion_p = count_inversions_recursive(P) - B, inversions_q = count_inversions_recursive(Q) - C, cross_inversions = _count_cross_inversions(A, B) + A, inversion_p = count_inversions_recursive(P) + B, inversions_q = count_inversions_recursive(Q) + C, cross_inversions = _count_cross_inversions(A, B) - num_inversions = inversion_p + inversions_q + cross_inversions - return C, num_inversions + num_inversions = inversion_p + inversions_q + cross_inversions + return C, num_inversions def _count_cross_inversions(P, Q): """ Counts the inversions across two sorted arrays. And combine the two arrays into one sorted array - For all 1<= i<=len(P) and for all 1 <= j <= len(Q), if P[i] > Q[j], then (i, j) is a cross inversion - Parameters ---------- P: array-like, sorted in non-decreasing order Q: array-like, sorted in non-decreasing order - Returns ------ R: array-like, a sorted array of the elements of `P` and `Q` num_inversion: int, the number of inversions across `P` and `Q` - Examples -------- - >>> _count_cross_inversions([1, 2, 3], [0, 2, 5]) ([0, 1, 2, 2, 3, 5], 4) >>> _count_cross_inversions([1, 2, 3], [3, 4, 5]) From 9feffe24b8fe90abffe5ad0c9ff9eca6faff9d1d Mon Sep 17 00:00:00 2001 From: Muhammad Hammad Sani <58339378+mhammadsaani@users.noreply.github.com> Date: Sat, 9 Oct 2021 12:20:02 +0500 Subject: [PATCH 2/4] Update divide_and_conquer/inversions.py Co-authored-by: John Law --- divide_and_conquer/inversions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/divide_and_conquer/inversions.py b/divide_and_conquer/inversions.py index 8b43bc011958..5f13d3e24d11 100644 --- a/divide_and_conquer/inversions.py +++ b/divide_and_conquer/inversions.py @@ -3,7 +3,7 @@ (i, j) for all 1 <= i < j <= n such that A[i] > A[j]? These pairs are called inversions. Counting the number of such inversions in an array-like object is the important. Among other things, counting inversions can help -us determine how close a given array is to being sorted +us determine how close a given array is to being sorted. In this implementation, I provide two algorithms, a divide-and-conquer algorithm which runs in nlogn and the brute-force n^2 algorithm. """ From 4fad726ba143c05c8aa1344fa6ab7bfa5544aa1c Mon Sep 17 00:00:00 2001 From: Muhammad Hammad Sani <58339378+mhammadsaani@users.noreply.github.com> Date: Sat, 9 Oct 2021 12:20:10 +0500 Subject: [PATCH 3/4] Update divide_and_conquer/inversions.py Co-authored-by: John Law --- divide_and_conquer/inversions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/divide_and_conquer/inversions.py b/divide_and_conquer/inversions.py index 5f13d3e24d11..ce16d200fe1f 100644 --- a/divide_and_conquer/inversions.py +++ b/divide_and_conquer/inversions.py @@ -2,7 +2,7 @@ Given an array-like data structure A[1..n], how many pairs (i, j) for all 1 <= i < j <= n such that A[i] > A[j]? These pairs are called inversions. Counting the number of such inversions in an array-like -object is the important. Among other things, counting inversions can help +object is the important. Among other things, counting inversions can help us determine how close a given array is to being sorted. In this implementation, I provide two algorithms, a divide-and-conquer algorithm which runs in nlogn and the brute-force n^2 algorithm. From 6c533254794920c11af3aba97936ce83c904771b Mon Sep 17 00:00:00 2001 From: Muhammad Hammad Sani <58339378+mhammadsaani@users.noreply.github.com> Date: Sat, 9 Oct 2021 12:20:19 +0500 Subject: [PATCH 4/4] Update divide_and_conquer/inversions.py Co-authored-by: John Law --- divide_and_conquer/inversions.py | 1 - 1 file changed, 1 deletion(-) diff --git a/divide_and_conquer/inversions.py b/divide_and_conquer/inversions.py index ce16d200fe1f..b471456025be 100644 --- a/divide_and_conquer/inversions.py +++ b/divide_and_conquer/inversions.py @@ -62,7 +62,6 @@ def count_inversions_recursive(arr): """ if len(arr) <= 1: return arr, 0 - mid = len(arr) // 2 P = arr[0:mid] Q = arr[mid:]