From d12949df6f1526ab3da8d02d6cc87c7f3aaccce0 Mon Sep 17 00:00:00 2001 From: ANANT JAIN <139585700+anant-jain01@users.noreply.github.com> Date: Fri, 11 Oct 2024 23:50:17 +0530 Subject: [PATCH 1/2] Create adaptive_merge_sort.py --- sorts/adaptive_merge_sort.py | 61 ++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 sorts/adaptive_merge_sort.py diff --git a/sorts/adaptive_merge_sort.py b/sorts/adaptive_merge_sort.py new file mode 100644 index 000000000000..e8fe3f1c4ede --- /dev/null +++ b/sorts/adaptive_merge_sort.py @@ -0,0 +1,61 @@ +""" +Adaptive Merge Sort implementation. +https://www.tutorialspoint.com/adaptive-merging-and-sorting-in-data-structure +""" + +from typing import List + +def adaptive_merge_sort(sequence: list) -> list: + """ + >>> adaptive_merge_sort([12, 11, 13, 5, 6, 7]) + [5, 6, 7, 11, 12, 13] + + >>> adaptive_merge_sort([4, 3, 2, 1]) + [1, 2, 3, 4] + + >>> adaptive_merge_sort(["apple", "zebra", "mango", "banana"]) + ['apple', 'banana', 'mango', 'zebra'] + """ + if len(sequence) < 2: + return sequence + + aux = sequence[:] + adaptive_merge_sort_recursive(sequence, aux, 0, len(sequence) - 1) + return sequence + +def adaptive_merge_sort_recursive(arr: list, aux: list, low: int, high: int) -> None: + if high <= low: + return + + mid = (low + high) // 2 + adaptive_merge_sort_recursive(aux, arr, low, mid) + adaptive_merge_sort_recursive(aux, arr, mid + 1, high) + + if arr[mid] <= arr[mid + 1]: + arr[low:high + 1] = aux[low:high + 1] + return + + merge(arr, aux, low, mid, high) + +def merge(arr: list, aux: list, low: int, mid: int, high: int) -> None: + i, j = low, mid + 1 + + for k in range(low, high + 1): + if i > mid: + aux[k] = arr[j] + j += 1 + elif j > high: + aux[k] = arr[i] + i += 1 + elif arr[j] < arr[i]: + aux[k] = arr[j] + j += 1 + else: + aux[k] = arr[i] + i += 1 + + +if __name__ == "__main__": + assert adaptive_merge_sort([12, 11, 13, 5, 6, 7]) == [5, 6, 7, 11, 12, 13] + assert adaptive_merge_sort([4, 3, 2, 1]) == [1, 2, 3, 4] + assert adaptive_merge_sort(["apple", "zebra", "mango", "banana"]) == ['apple', 'banana', 'mango', 'zebra'] From b7ecd16f0fb88e047b49c0f05a34d1921f0fb813 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 11 Oct 2024 18:21:11 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- sorts/adaptive_merge_sort.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/sorts/adaptive_merge_sort.py b/sorts/adaptive_merge_sort.py index e8fe3f1c4ede..7493763eac8b 100644 --- a/sorts/adaptive_merge_sort.py +++ b/sorts/adaptive_merge_sort.py @@ -5,6 +5,7 @@ from typing import List + def adaptive_merge_sort(sequence: list) -> list: """ >>> adaptive_merge_sort([12, 11, 13, 5, 6, 7]) @@ -23,6 +24,7 @@ def adaptive_merge_sort(sequence: list) -> list: adaptive_merge_sort_recursive(sequence, aux, 0, len(sequence) - 1) return sequence + def adaptive_merge_sort_recursive(arr: list, aux: list, low: int, high: int) -> None: if high <= low: return @@ -32,11 +34,12 @@ def adaptive_merge_sort_recursive(arr: list, aux: list, low: int, high: int) -> adaptive_merge_sort_recursive(aux, arr, mid + 1, high) if arr[mid] <= arr[mid + 1]: - arr[low:high + 1] = aux[low:high + 1] + arr[low : high + 1] = aux[low : high + 1] return merge(arr, aux, low, mid, high) + def merge(arr: list, aux: list, low: int, mid: int, high: int) -> None: i, j = low, mid + 1 @@ -58,4 +61,9 @@ def merge(arr: list, aux: list, low: int, mid: int, high: int) -> None: if __name__ == "__main__": assert adaptive_merge_sort([12, 11, 13, 5, 6, 7]) == [5, 6, 7, 11, 12, 13] assert adaptive_merge_sort([4, 3, 2, 1]) == [1, 2, 3, 4] - assert adaptive_merge_sort(["apple", "zebra", "mango", "banana"]) == ['apple', 'banana', 'mango', 'zebra'] + assert adaptive_merge_sort(["apple", "zebra", "mango", "banana"]) == [ + "apple", + "banana", + "mango", + "zebra", + ]