From 451d4e14e290c596ee7c38f342fd7725fae3c459 Mon Sep 17 00:00:00 2001 From: ANANT JAIN <139585700+anant-jain01@users.noreply.github.com> Date: Sat, 12 Oct 2024 00:06:23 +0530 Subject: [PATCH 01/24] Create adaptive_merge_sort.py --- sorts/adaptive_merge_sort.py | 51 ++++++++++++++++++++++++++++++++++++ 1 file changed, 51 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..a3a7f4dc22af --- /dev/null +++ b/sorts/adaptive_merge_sort.py @@ -0,0 +1,51 @@ +def adaptive_merge_sort(sequence: list) -> list: + """ + Sorts a list using the Adaptive Merge Sort algorithm. + """ + if len(sequence) < 2: + return sequence + + aux = sequence[:] + print(f"Initial sequence: {sequence}") + adaptive_merge_sort_helper(sequence, aux, 0, len(sequence) - 1) + print(f"Sorted sequence: {sequence}") + return sequence + + +def adaptive_merge_sort_helper(array: list, aux: list, low: int, high: int): + if high <= low: + return + + mid = (low + high) // 2 + print(f"Sorting: array[{low}:{mid + 1}] and array[{mid + 1}:{high + 1}]") + + adaptive_merge_sort_helper(aux, array, low, mid) + adaptive_merge_sort_helper(aux, array, mid + 1, high) + + if array[mid] <= array[mid + 1]: + print(f"Skipping merge as array[{mid}] <= array[{mid + 1}]") + array[low:high + 1] = aux[low:high + 1] + return + + merge(array, aux, low, mid, high) + + +def merge(array: list, aux: list, low: int, mid: int, high: int): + print(f"Merging: array[{low}:{mid + 1}] and array[{mid + 1}:{high + 1}]") + i, j = low, mid + 1 + + for k in range(low, high + 1): + if i > mid: + aux[k] = array[j] + j += 1 + elif j > high: + aux[k] = array[i] + i += 1 + elif array[j] < array[i]: + aux[k] = array[j] + j += 1 + else: + aux[k] = array[i] + i += 1 + + print(f"After merge: {aux[low:high + 1]}") From 97aa418474cf5e1fa7762f2f77726e223c2865f2 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:37:04 +0000 Subject: [PATCH 02/24] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- sorts/adaptive_merge_sort.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sorts/adaptive_merge_sort.py b/sorts/adaptive_merge_sort.py index a3a7f4dc22af..6c44baf8072e 100644 --- a/sorts/adaptive_merge_sort.py +++ b/sorts/adaptive_merge_sort.py @@ -24,7 +24,7 @@ def adaptive_merge_sort_helper(array: list, aux: list, low: int, high: int): if array[mid] <= array[mid + 1]: print(f"Skipping merge as array[{mid}] <= array[{mid + 1}]") - array[low:high + 1] = aux[low:high + 1] + array[low : high + 1] = aux[low : high + 1] return merge(array, aux, low, mid, high) From 8d5ecd68830a8c0514cff86e1d96deef54689ef9 Mon Sep 17 00:00:00 2001 From: ANANT JAIN <139585700+anant-jain01@users.noreply.github.com> Date: Thu, 17 Oct 2024 00:57:11 +0530 Subject: [PATCH 03/24] Update adaptive_merge_sort.py --- sorts/adaptive_merge_sort.py | 42 +++++++++++++++++++++++++++--------- 1 file changed, 32 insertions(+), 10 deletions(-) diff --git a/sorts/adaptive_merge_sort.py b/sorts/adaptive_merge_sort.py index 6c44baf8072e..07cb459e43f4 100644 --- a/sorts/adaptive_merge_sort.py +++ b/sorts/adaptive_merge_sort.py @@ -1,39 +1,62 @@ def adaptive_merge_sort(sequence: list) -> list: """ Sorts a list using the Adaptive Merge Sort algorithm. + + >>> adaptive_merge_sort([4, 3, 1, 2]) + Initial sequence: [4, 3, 1, 2] + Sorting: array[0:2] and array[2:4] + Sorting: array[0:1] and array[1:2] + Sorting: array[2:3] and array[3:4] + Skipping merge as array[0] <= array[1] + Skipping merge as array[2] <= array[3] + Merging: array[0:2] and array[2:4] + After merge: [1, 2, 3, 4] + Sorted sequence: [1, 2, 3, 4] + [1, 2, 3, 4] """ if len(sequence) < 2: return sequence - aux = sequence[:] print(f"Initial sequence: {sequence}") adaptive_merge_sort_helper(sequence, aux, 0, len(sequence) - 1) print(f"Sorted sequence: {sequence}") return sequence - -def adaptive_merge_sort_helper(array: list, aux: list, low: int, high: int): +def adaptive_merge_sort_helper(array: list, aux: list, low: int, high: int) -> None: + """ + Helper function for Adaptive Merge Sort algorithm. + + >>> adaptive_merge_sort_helper([4, 3, 1, 2], [4, 3, 1, 2], 0, 3) + Sorting: array[0:2] and array[2:4] + Sorting: array[0:1] and array[1:2] + Sorting: array[2:3] and array[3:4] + Skipping merge as array[0] <= array[1] + Skipping merge as array[2] <= array[3] + Merging: array[0:2] and array[2:4] + After merge: [1, 2, 3, 4] + """ if high <= low: return - mid = (low + high) // 2 print(f"Sorting: array[{low}:{mid + 1}] and array[{mid + 1}:{high + 1}]") - adaptive_merge_sort_helper(aux, array, low, mid) adaptive_merge_sort_helper(aux, array, mid + 1, high) - if array[mid] <= array[mid + 1]: print(f"Skipping merge as array[{mid}] <= array[{mid + 1}]") array[low : high + 1] = aux[low : high + 1] return - merge(array, aux, low, mid, high) +def merge(array: list, aux: list, low: int, mid: int, high: int) -> None: + """ + Merges two sorted subarrays of the main array. -def merge(array: list, aux: list, low: int, mid: int, high: int): + >>> merge([4, 3, 1, 2], [4, 3, 1, 2], 0, 1, 3) + Merging: array[0:2] and array[2:4] + After merge: [1, 2, 3, 4] + """ print(f"Merging: array[{low}:{mid + 1}] and array[{mid + 1}:{high + 1}]") i, j = low, mid + 1 - for k in range(low, high + 1): if i > mid: aux[k] = array[j] @@ -47,5 +70,4 @@ def merge(array: list, aux: list, low: int, mid: int, high: int): else: aux[k] = array[i] i += 1 - print(f"After merge: {aux[low:high + 1]}") From f046da9a8c83bfa368f58dba89f4e111e9366858 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 16 Oct 2024 19:28:09 +0000 Subject: [PATCH 04/24] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- sorts/adaptive_merge_sort.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/sorts/adaptive_merge_sort.py b/sorts/adaptive_merge_sort.py index 07cb459e43f4..9cbee4b408f5 100644 --- a/sorts/adaptive_merge_sort.py +++ b/sorts/adaptive_merge_sort.py @@ -1,7 +1,7 @@ def adaptive_merge_sort(sequence: list) -> list: """ Sorts a list using the Adaptive Merge Sort algorithm. - + >>> adaptive_merge_sort([4, 3, 1, 2]) Initial sequence: [4, 3, 1, 2] Sorting: array[0:2] and array[2:4] @@ -22,10 +22,11 @@ def adaptive_merge_sort(sequence: list) -> list: print(f"Sorted sequence: {sequence}") return sequence + def adaptive_merge_sort_helper(array: list, aux: list, low: int, high: int) -> None: """ Helper function for Adaptive Merge Sort algorithm. - + >>> adaptive_merge_sort_helper([4, 3, 1, 2], [4, 3, 1, 2], 0, 3) Sorting: array[0:2] and array[2:4] Sorting: array[0:1] and array[1:2] @@ -47,6 +48,7 @@ def adaptive_merge_sort_helper(array: list, aux: list, low: int, high: int) -> N return merge(array, aux, low, mid, high) + def merge(array: list, aux: list, low: int, mid: int, high: int) -> None: """ Merges two sorted subarrays of the main array. From 94afed0f1dbfd9ebcea57c1f83a32a25146c946a Mon Sep 17 00:00:00 2001 From: ANANT JAIN <139585700+anant-jain01@users.noreply.github.com> Date: Thu, 17 Oct 2024 00:58:46 +0530 Subject: [PATCH 05/24] Update adaptive_merge_sort.py --- sorts/adaptive_merge_sort.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/sorts/adaptive_merge_sort.py b/sorts/adaptive_merge_sort.py index 9cbee4b408f5..07cb459e43f4 100644 --- a/sorts/adaptive_merge_sort.py +++ b/sorts/adaptive_merge_sort.py @@ -1,7 +1,7 @@ def adaptive_merge_sort(sequence: list) -> list: """ Sorts a list using the Adaptive Merge Sort algorithm. - + >>> adaptive_merge_sort([4, 3, 1, 2]) Initial sequence: [4, 3, 1, 2] Sorting: array[0:2] and array[2:4] @@ -22,11 +22,10 @@ def adaptive_merge_sort(sequence: list) -> list: print(f"Sorted sequence: {sequence}") return sequence - def adaptive_merge_sort_helper(array: list, aux: list, low: int, high: int) -> None: """ Helper function for Adaptive Merge Sort algorithm. - + >>> adaptive_merge_sort_helper([4, 3, 1, 2], [4, 3, 1, 2], 0, 3) Sorting: array[0:2] and array[2:4] Sorting: array[0:1] and array[1:2] @@ -48,7 +47,6 @@ def adaptive_merge_sort_helper(array: list, aux: list, low: int, high: int) -> N return merge(array, aux, low, mid, high) - def merge(array: list, aux: list, low: int, mid: int, high: int) -> None: """ Merges two sorted subarrays of the main array. From cd6fca872b9136cd0d0b56255efb69c32769e2a3 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 16 Oct 2024 19:29:28 +0000 Subject: [PATCH 06/24] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- sorts/adaptive_merge_sort.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/sorts/adaptive_merge_sort.py b/sorts/adaptive_merge_sort.py index 07cb459e43f4..9cbee4b408f5 100644 --- a/sorts/adaptive_merge_sort.py +++ b/sorts/adaptive_merge_sort.py @@ -1,7 +1,7 @@ def adaptive_merge_sort(sequence: list) -> list: """ Sorts a list using the Adaptive Merge Sort algorithm. - + >>> adaptive_merge_sort([4, 3, 1, 2]) Initial sequence: [4, 3, 1, 2] Sorting: array[0:2] and array[2:4] @@ -22,10 +22,11 @@ def adaptive_merge_sort(sequence: list) -> list: print(f"Sorted sequence: {sequence}") return sequence + def adaptive_merge_sort_helper(array: list, aux: list, low: int, high: int) -> None: """ Helper function for Adaptive Merge Sort algorithm. - + >>> adaptive_merge_sort_helper([4, 3, 1, 2], [4, 3, 1, 2], 0, 3) Sorting: array[0:2] and array[2:4] Sorting: array[0:1] and array[1:2] @@ -47,6 +48,7 @@ def adaptive_merge_sort_helper(array: list, aux: list, low: int, high: int) -> N return merge(array, aux, low, mid, high) + def merge(array: list, aux: list, low: int, mid: int, high: int) -> None: """ Merges two sorted subarrays of the main array. From 4a0f090f54278931598d21a3e08e041c02b386e5 Mon Sep 17 00:00:00 2001 From: ANANT JAIN <139585700+anant-jain01@users.noreply.github.com> Date: Thu, 17 Oct 2024 01:04:07 +0530 Subject: [PATCH 07/24] Update adaptive_merge_sort.py --- sorts/adaptive_merge_sort.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/sorts/adaptive_merge_sort.py b/sorts/adaptive_merge_sort.py index 9cbee4b408f5..07cb459e43f4 100644 --- a/sorts/adaptive_merge_sort.py +++ b/sorts/adaptive_merge_sort.py @@ -1,7 +1,7 @@ def adaptive_merge_sort(sequence: list) -> list: """ Sorts a list using the Adaptive Merge Sort algorithm. - + >>> adaptive_merge_sort([4, 3, 1, 2]) Initial sequence: [4, 3, 1, 2] Sorting: array[0:2] and array[2:4] @@ -22,11 +22,10 @@ def adaptive_merge_sort(sequence: list) -> list: print(f"Sorted sequence: {sequence}") return sequence - def adaptive_merge_sort_helper(array: list, aux: list, low: int, high: int) -> None: """ Helper function for Adaptive Merge Sort algorithm. - + >>> adaptive_merge_sort_helper([4, 3, 1, 2], [4, 3, 1, 2], 0, 3) Sorting: array[0:2] and array[2:4] Sorting: array[0:1] and array[1:2] @@ -48,7 +47,6 @@ def adaptive_merge_sort_helper(array: list, aux: list, low: int, high: int) -> N return merge(array, aux, low, mid, high) - def merge(array: list, aux: list, low: int, mid: int, high: int) -> None: """ Merges two sorted subarrays of the main array. From b17141a342bf7c882e17f2d3699cf73b90df51f9 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 16 Oct 2024 19:34:29 +0000 Subject: [PATCH 08/24] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- sorts/adaptive_merge_sort.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/sorts/adaptive_merge_sort.py b/sorts/adaptive_merge_sort.py index 07cb459e43f4..9cbee4b408f5 100644 --- a/sorts/adaptive_merge_sort.py +++ b/sorts/adaptive_merge_sort.py @@ -1,7 +1,7 @@ def adaptive_merge_sort(sequence: list) -> list: """ Sorts a list using the Adaptive Merge Sort algorithm. - + >>> adaptive_merge_sort([4, 3, 1, 2]) Initial sequence: [4, 3, 1, 2] Sorting: array[0:2] and array[2:4] @@ -22,10 +22,11 @@ def adaptive_merge_sort(sequence: list) -> list: print(f"Sorted sequence: {sequence}") return sequence + def adaptive_merge_sort_helper(array: list, aux: list, low: int, high: int) -> None: """ Helper function for Adaptive Merge Sort algorithm. - + >>> adaptive_merge_sort_helper([4, 3, 1, 2], [4, 3, 1, 2], 0, 3) Sorting: array[0:2] and array[2:4] Sorting: array[0:1] and array[1:2] @@ -47,6 +48,7 @@ def adaptive_merge_sort_helper(array: list, aux: list, low: int, high: int) -> N return merge(array, aux, low, mid, high) + def merge(array: list, aux: list, low: int, mid: int, high: int) -> None: """ Merges two sorted subarrays of the main array. From 513ed29db5b1411f315072fd49d9b012519e4b01 Mon Sep 17 00:00:00 2001 From: ANANT JAIN <139585700+anant-jain01@users.noreply.github.com> Date: Thu, 17 Oct 2024 01:13:14 +0530 Subject: [PATCH 09/24] Update adaptive_merge_sort.py --- sorts/adaptive_merge_sort.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/sorts/adaptive_merge_sort.py b/sorts/adaptive_merge_sort.py index 9cbee4b408f5..78b0b3b165c7 100644 --- a/sorts/adaptive_merge_sort.py +++ b/sorts/adaptive_merge_sort.py @@ -22,7 +22,6 @@ def adaptive_merge_sort(sequence: list) -> list: print(f"Sorted sequence: {sequence}") return sequence - def adaptive_merge_sort_helper(array: list, aux: list, low: int, high: int) -> None: """ Helper function for Adaptive Merge Sort algorithm. @@ -48,7 +47,6 @@ def adaptive_merge_sort_helper(array: list, aux: list, low: int, high: int) -> N return merge(array, aux, low, mid, high) - def merge(array: list, aux: list, low: int, mid: int, high: int) -> None: """ Merges two sorted subarrays of the main array. From 3cabfc60828b5cfbd0cbf31116d0ae33347ca88e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 16 Oct 2024 19:43:49 +0000 Subject: [PATCH 10/24] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- sorts/adaptive_merge_sort.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/sorts/adaptive_merge_sort.py b/sorts/adaptive_merge_sort.py index 78b0b3b165c7..9cbee4b408f5 100644 --- a/sorts/adaptive_merge_sort.py +++ b/sorts/adaptive_merge_sort.py @@ -22,6 +22,7 @@ def adaptive_merge_sort(sequence: list) -> list: print(f"Sorted sequence: {sequence}") return sequence + def adaptive_merge_sort_helper(array: list, aux: list, low: int, high: int) -> None: """ Helper function for Adaptive Merge Sort algorithm. @@ -47,6 +48,7 @@ def adaptive_merge_sort_helper(array: list, aux: list, low: int, high: int) -> N return merge(array, aux, low, mid, high) + def merge(array: list, aux: list, low: int, mid: int, high: int) -> None: """ Merges two sorted subarrays of the main array. From cb4acf52e32faecd55df57ae28ec4c57c0b261ee Mon Sep 17 00:00:00 2001 From: ANANT JAIN <139585700+anant-jain01@users.noreply.github.com> Date: Thu, 17 Oct 2024 01:18:06 +0530 Subject: [PATCH 11/24] Update adaptive_merge_sort.py --- sorts/adaptive_merge_sort.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/sorts/adaptive_merge_sort.py b/sorts/adaptive_merge_sort.py index 9cbee4b408f5..60359d2983db 100644 --- a/sorts/adaptive_merge_sort.py +++ b/sorts/adaptive_merge_sort.py @@ -6,6 +6,8 @@ def adaptive_merge_sort(sequence: list) -> list: Initial sequence: [4, 3, 1, 2] Sorting: array[0:2] and array[2:4] Sorting: array[0:1] and array[1:2] + Merging: array[0:1] and array[1:2] + After merge: [3, 4] Sorting: array[2:3] and array[3:4] Skipping merge as array[0] <= array[1] Skipping merge as array[2] <= array[3] @@ -22,7 +24,6 @@ def adaptive_merge_sort(sequence: list) -> list: print(f"Sorted sequence: {sequence}") return sequence - def adaptive_merge_sort_helper(array: list, aux: list, low: int, high: int) -> None: """ Helper function for Adaptive Merge Sort algorithm. @@ -30,8 +31,9 @@ def adaptive_merge_sort_helper(array: list, aux: list, low: int, high: int) -> N >>> adaptive_merge_sort_helper([4, 3, 1, 2], [4, 3, 1, 2], 0, 3) Sorting: array[0:2] and array[2:4] Sorting: array[0:1] and array[1:2] + Merging: array[0:1] and array[1:2] + After merge: [3, 4] Sorting: array[2:3] and array[3:4] - Skipping merge as array[0] <= array[1] Skipping merge as array[2] <= array[3] Merging: array[0:2] and array[2:4] After merge: [1, 2, 3, 4] @@ -48,7 +50,6 @@ def adaptive_merge_sort_helper(array: list, aux: list, low: int, high: int) -> N return merge(array, aux, low, mid, high) - def merge(array: list, aux: list, low: int, mid: int, high: int) -> None: """ Merges two sorted subarrays of the main array. @@ -72,4 +73,5 @@ def merge(array: list, aux: list, low: int, mid: int, high: int) -> None: else: aux[k] = array[i] i += 1 + array[low:high + 1] = aux[low:high + 1] # Update the main array with merged values print(f"After merge: {aux[low:high + 1]}") From 15578e80c647eab0f2ea98195fa886d1daf21613 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 16 Oct 2024 19:49:10 +0000 Subject: [PATCH 12/24] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- sorts/adaptive_merge_sort.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/sorts/adaptive_merge_sort.py b/sorts/adaptive_merge_sort.py index 60359d2983db..37119158cf34 100644 --- a/sorts/adaptive_merge_sort.py +++ b/sorts/adaptive_merge_sort.py @@ -24,6 +24,7 @@ def adaptive_merge_sort(sequence: list) -> list: print(f"Sorted sequence: {sequence}") return sequence + def adaptive_merge_sort_helper(array: list, aux: list, low: int, high: int) -> None: """ Helper function for Adaptive Merge Sort algorithm. @@ -50,6 +51,7 @@ def adaptive_merge_sort_helper(array: list, aux: list, low: int, high: int) -> N return merge(array, aux, low, mid, high) + def merge(array: list, aux: list, low: int, mid: int, high: int) -> None: """ Merges two sorted subarrays of the main array. @@ -73,5 +75,7 @@ def merge(array: list, aux: list, low: int, mid: int, high: int) -> None: else: aux[k] = array[i] i += 1 - array[low:high + 1] = aux[low:high + 1] # Update the main array with merged values + array[low : high + 1] = aux[ + low : high + 1 + ] # Update the main array with merged values print(f"After merge: {aux[low:high + 1]}") From f331afd6e349dc2c5560e75c411481ef03cc7813 Mon Sep 17 00:00:00 2001 From: ANANT JAIN <139585700+anant-jain01@users.noreply.github.com> Date: Thu, 17 Oct 2024 01:25:53 +0530 Subject: [PATCH 13/24] Update adaptive_merge_sort.py --- sorts/adaptive_merge_sort.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/sorts/adaptive_merge_sort.py b/sorts/adaptive_merge_sort.py index 37119158cf34..fd7ad8fdb504 100644 --- a/sorts/adaptive_merge_sort.py +++ b/sorts/adaptive_merge_sort.py @@ -9,7 +9,6 @@ def adaptive_merge_sort(sequence: list) -> list: Merging: array[0:1] and array[1:2] After merge: [3, 4] Sorting: array[2:3] and array[3:4] - Skipping merge as array[0] <= array[1] Skipping merge as array[2] <= array[3] Merging: array[0:2] and array[2:4] After merge: [1, 2, 3, 4] @@ -24,7 +23,6 @@ def adaptive_merge_sort(sequence: list) -> list: print(f"Sorted sequence: {sequence}") return sequence - def adaptive_merge_sort_helper(array: list, aux: list, low: int, high: int) -> None: """ Helper function for Adaptive Merge Sort algorithm. @@ -51,7 +49,6 @@ def adaptive_merge_sort_helper(array: list, aux: list, low: int, high: int) -> N return merge(array, aux, low, mid, high) - def merge(array: list, aux: list, low: int, mid: int, high: int) -> None: """ Merges two sorted subarrays of the main array. @@ -75,7 +72,7 @@ def merge(array: list, aux: list, low: int, mid: int, high: int) -> None: else: aux[k] = array[i] i += 1 - array[low : high + 1] = aux[ - low : high + 1 - ] # Update the main array with merged values + # Ensure we correctly copy back to the main array + for k in range(low, high + 1): + array[k] = aux[k] print(f"After merge: {aux[low:high + 1]}") From eb3284d6f0fdf22e3d82bce431aef0fc3938990c Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 16 Oct 2024 19:56:16 +0000 Subject: [PATCH 14/24] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- sorts/adaptive_merge_sort.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/sorts/adaptive_merge_sort.py b/sorts/adaptive_merge_sort.py index fd7ad8fdb504..3a29d110f34f 100644 --- a/sorts/adaptive_merge_sort.py +++ b/sorts/adaptive_merge_sort.py @@ -23,6 +23,7 @@ def adaptive_merge_sort(sequence: list) -> list: print(f"Sorted sequence: {sequence}") return sequence + def adaptive_merge_sort_helper(array: list, aux: list, low: int, high: int) -> None: """ Helper function for Adaptive Merge Sort algorithm. @@ -49,6 +50,7 @@ def adaptive_merge_sort_helper(array: list, aux: list, low: int, high: int) -> N return merge(array, aux, low, mid, high) + def merge(array: list, aux: list, low: int, mid: int, high: int) -> None: """ Merges two sorted subarrays of the main array. From c8397d47cf161104a7463d563a69a81a2b87b1a8 Mon Sep 17 00:00:00 2001 From: ANANT JAIN <139585700+anant-jain01@users.noreply.github.com> Date: Thu, 17 Oct 2024 01:34:31 +0530 Subject: [PATCH 15/24] Update adaptive_merge_sort.py --- sorts/adaptive_merge_sort.py | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/sorts/adaptive_merge_sort.py b/sorts/adaptive_merge_sort.py index 3a29d110f34f..4ce64f3704be 100644 --- a/sorts/adaptive_merge_sort.py +++ b/sorts/adaptive_merge_sort.py @@ -1,4 +1,4 @@ -def adaptive_merge_sort(sequence: list) -> list: + def adaptive_merge_sort(sequence: list) -> list: """ Sorts a list using the Adaptive Merge Sort algorithm. @@ -17,6 +17,7 @@ def adaptive_merge_sort(sequence: list) -> list: """ if len(sequence) < 2: return sequence + aux = sequence[:] print(f"Initial sequence: {sequence}") adaptive_merge_sort_helper(sequence, aux, 0, len(sequence) - 1) @@ -40,14 +41,18 @@ def adaptive_merge_sort_helper(array: list, aux: list, low: int, high: int) -> N """ if high <= low: return + mid = (low + high) // 2 print(f"Sorting: array[{low}:{mid + 1}] and array[{mid + 1}:{high + 1}]") + adaptive_merge_sort_helper(aux, array, low, mid) adaptive_merge_sort_helper(aux, array, mid + 1, high) + if array[mid] <= array[mid + 1]: print(f"Skipping merge as array[{mid}] <= array[{mid + 1}]") - array[low : high + 1] = aux[low : high + 1] + array[low:high + 1] = aux[low:high + 1] return + merge(array, aux, low, mid, high) @@ -60,6 +65,7 @@ def merge(array: list, aux: list, low: int, mid: int, high: int) -> None: After merge: [1, 2, 3, 4] """ print(f"Merging: array[{low}:{mid + 1}] and array[{mid + 1}:{high + 1}]") + i, j = low, mid + 1 for k in range(low, high + 1): if i > mid: @@ -68,13 +74,19 @@ def merge(array: list, aux: list, low: int, mid: int, high: int) -> None: elif j > high: aux[k] = array[i] i += 1 - elif array[j] < array[i]: - aux[k] = array[j] - j += 1 - else: + elif array[i] <= array[j]: # Keep stable by using <= aux[k] = array[i] i += 1 - # Ensure we correctly copy back to the main array + else: + aux[k] = array[j] + j += 1 + for k in range(low, high + 1): array[k] = aux[k] - print(f"After merge: {aux[low:high + 1]}") + + print(f"After merge: {array[low:high + 1]}") + + +# Example usage +if __name__ == "__main__": + print(adaptive_merge_sort([4, 3, 1, 2])) From ddfcaecb7718b71b8de6fa5a183a9218f69db06d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 16 Oct 2024 20:04:45 +0000 Subject: [PATCH 16/24] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- sorts/adaptive_merge_sort.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/sorts/adaptive_merge_sort.py b/sorts/adaptive_merge_sort.py index 4ce64f3704be..d8cde73887b0 100644 --- a/sorts/adaptive_merge_sort.py +++ b/sorts/adaptive_merge_sort.py @@ -17,7 +17,7 @@ def adaptive_merge_sort(sequence: list) -> list: """ if len(sequence) < 2: return sequence - + aux = sequence[:] print(f"Initial sequence: {sequence}") adaptive_merge_sort_helper(sequence, aux, 0, len(sequence) - 1) @@ -41,18 +41,18 @@ def adaptive_merge_sort_helper(array: list, aux: list, low: int, high: int) -> N """ if high <= low: return - + mid = (low + high) // 2 print(f"Sorting: array[{low}:{mid + 1}] and array[{mid + 1}:{high + 1}]") - + adaptive_merge_sort_helper(aux, array, low, mid) adaptive_merge_sort_helper(aux, array, mid + 1, high) - + if array[mid] <= array[mid + 1]: print(f"Skipping merge as array[{mid}] <= array[{mid + 1}]") array[low:high + 1] = aux[low:high + 1] return - + merge(array, aux, low, mid, high) @@ -65,7 +65,7 @@ def merge(array: list, aux: list, low: int, mid: int, high: int) -> None: After merge: [1, 2, 3, 4] """ print(f"Merging: array[{low}:{mid + 1}] and array[{mid + 1}:{high + 1}]") - + i, j = low, mid + 1 for k in range(low, high + 1): if i > mid: @@ -80,10 +80,10 @@ def merge(array: list, aux: list, low: int, mid: int, high: int) -> None: else: aux[k] = array[j] j += 1 - + for k in range(low, high + 1): array[k] = aux[k] - + print(f"After merge: {array[low:high + 1]}") From b08a35c5d82a80fc80249b2c86d9db5f96d635c0 Mon Sep 17 00:00:00 2001 From: ANANT JAIN <139585700+anant-jain01@users.noreply.github.com> Date: Thu, 17 Oct 2024 01:37:38 +0530 Subject: [PATCH 17/24] Update adaptive_merge_sort.py --- sorts/adaptive_merge_sort.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sorts/adaptive_merge_sort.py b/sorts/adaptive_merge_sort.py index d8cde73887b0..4107be7be1bb 100644 --- a/sorts/adaptive_merge_sort.py +++ b/sorts/adaptive_merge_sort.py @@ -1,4 +1,4 @@ - def adaptive_merge_sort(sequence: list) -> list: +def adaptive_merge_sort(sequence: list) -> list: """ Sorts a list using the Adaptive Merge Sort algorithm. From ad7fbab066680c6e909954d78313109f4d583711 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 16 Oct 2024 20:08:01 +0000 Subject: [PATCH 18/24] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- sorts/adaptive_merge_sort.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sorts/adaptive_merge_sort.py b/sorts/adaptive_merge_sort.py index 4107be7be1bb..a34a11e37498 100644 --- a/sorts/adaptive_merge_sort.py +++ b/sorts/adaptive_merge_sort.py @@ -50,7 +50,7 @@ def adaptive_merge_sort_helper(array: list, aux: list, low: int, high: int) -> N if array[mid] <= array[mid + 1]: print(f"Skipping merge as array[{mid}] <= array[{mid + 1}]") - array[low:high + 1] = aux[low:high + 1] + array[low : high + 1] = aux[low : high + 1] return merge(array, aux, low, mid, high) From 952dc0a4ed1c33d8bf7c0ba4c135901cb9a53362 Mon Sep 17 00:00:00 2001 From: ANANT JAIN <139585700+anant-jain01@users.noreply.github.com> Date: Thu, 17 Oct 2024 01:41:31 +0530 Subject: [PATCH 19/24] Update adaptive_merge_sort.py --- sorts/adaptive_merge_sort.py | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/sorts/adaptive_merge_sort.py b/sorts/adaptive_merge_sort.py index a34a11e37498..caa76b74688e 100644 --- a/sorts/adaptive_merge_sort.py +++ b/sorts/adaptive_merge_sort.py @@ -50,7 +50,7 @@ def adaptive_merge_sort_helper(array: list, aux: list, low: int, high: int) -> N if array[mid] <= array[mid + 1]: print(f"Skipping merge as array[{mid}] <= array[{mid + 1}]") - array[low : high + 1] = aux[low : high + 1] + array[low:high + 1] = aux[low:high + 1] return merge(array, aux, low, mid, high) @@ -68,18 +68,12 @@ def merge(array: 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: + if i > mid or (j <= high and array[j] < array[i]): aux[k] = array[j] j += 1 - elif j > high: - aux[k] = array[i] - i += 1 - elif array[i] <= array[j]: # Keep stable by using <= + else: aux[k] = array[i] i += 1 - else: - aux[k] = array[j] - j += 1 for k in range(low, high + 1): array[k] = aux[k] From 7d3d1316c5021ac635c78a162f3d97c374420fe3 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 16 Oct 2024 20:12:33 +0000 Subject: [PATCH 20/24] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- sorts/adaptive_merge_sort.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sorts/adaptive_merge_sort.py b/sorts/adaptive_merge_sort.py index caa76b74688e..fccbe6552789 100644 --- a/sorts/adaptive_merge_sort.py +++ b/sorts/adaptive_merge_sort.py @@ -50,7 +50,7 @@ def adaptive_merge_sort_helper(array: list, aux: list, low: int, high: int) -> N if array[mid] <= array[mid + 1]: print(f"Skipping merge as array[{mid}] <= array[{mid + 1}]") - array[low:high + 1] = aux[low:high + 1] + array[low : high + 1] = aux[low : high + 1] return merge(array, aux, low, mid, high) From b82503ee4abd927dc4808fcfde13b68b7c778fde Mon Sep 17 00:00:00 2001 From: ANANT JAIN <139585700+anant-jain01@users.noreply.github.com> Date: Thu, 17 Oct 2024 01:52:29 +0530 Subject: [PATCH 21/24] Update adaptive_merge_sort.py --- sorts/adaptive_merge_sort.py | 73 ++++-------------------------------- 1 file changed, 8 insertions(+), 65 deletions(-) diff --git a/sorts/adaptive_merge_sort.py b/sorts/adaptive_merge_sort.py index fccbe6552789..02621d14a86f 100644 --- a/sorts/adaptive_merge_sort.py +++ b/sorts/adaptive_merge_sort.py @@ -1,61 +1,3 @@ -def adaptive_merge_sort(sequence: list) -> list: - """ - Sorts a list using the Adaptive Merge Sort algorithm. - - >>> adaptive_merge_sort([4, 3, 1, 2]) - Initial sequence: [4, 3, 1, 2] - Sorting: array[0:2] and array[2:4] - Sorting: array[0:1] and array[1:2] - Merging: array[0:1] and array[1:2] - After merge: [3, 4] - Sorting: array[2:3] and array[3:4] - Skipping merge as array[2] <= array[3] - Merging: array[0:2] and array[2:4] - After merge: [1, 2, 3, 4] - Sorted sequence: [1, 2, 3, 4] - [1, 2, 3, 4] - """ - if len(sequence) < 2: - return sequence - - aux = sequence[:] - print(f"Initial sequence: {sequence}") - adaptive_merge_sort_helper(sequence, aux, 0, len(sequence) - 1) - print(f"Sorted sequence: {sequence}") - return sequence - - -def adaptive_merge_sort_helper(array: list, aux: list, low: int, high: int) -> None: - """ - Helper function for Adaptive Merge Sort algorithm. - - >>> adaptive_merge_sort_helper([4, 3, 1, 2], [4, 3, 1, 2], 0, 3) - Sorting: array[0:2] and array[2:4] - Sorting: array[0:1] and array[1:2] - Merging: array[0:1] and array[1:2] - After merge: [3, 4] - Sorting: array[2:3] and array[3:4] - Skipping merge as array[2] <= array[3] - Merging: array[0:2] and array[2:4] - After merge: [1, 2, 3, 4] - """ - if high <= low: - return - - mid = (low + high) // 2 - print(f"Sorting: array[{low}:{mid + 1}] and array[{mid + 1}:{high + 1}]") - - adaptive_merge_sort_helper(aux, array, low, mid) - adaptive_merge_sort_helper(aux, array, mid + 1, high) - - if array[mid] <= array[mid + 1]: - print(f"Skipping merge as array[{mid}] <= array[{mid + 1}]") - array[low : high + 1] = aux[low : high + 1] - return - - merge(array, aux, low, mid, high) - - def merge(array: list, aux: list, low: int, mid: int, high: int) -> None: """ Merges two sorted subarrays of the main array. @@ -68,19 +10,20 @@ def merge(array: 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 or (j <= high and array[j] < array[i]): + if i > mid: aux[k] = array[j] j += 1 - else: + elif j > high: aux[k] = array[i] i += 1 + elif array[i] <= array[j]: + aux[k] = array[i] + i += 1 + else: + aux[k] = array[j] + j += 1 for k in range(low, high + 1): array[k] = aux[k] print(f"After merge: {array[low:high + 1]}") - - -# Example usage -if __name__ == "__main__": - print(adaptive_merge_sort([4, 3, 1, 2])) From 29f60d10f9e79c6522138de44b4f0a6dd13dac8e Mon Sep 17 00:00:00 2001 From: ANANT JAIN <139585700+anant-jain01@users.noreply.github.com> Date: Thu, 17 Oct 2024 01:53:28 +0530 Subject: [PATCH 22/24] Update adaptive_merge_sort.py --- sorts/adaptive_merge_sort.py | 63 ++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/sorts/adaptive_merge_sort.py b/sorts/adaptive_merge_sort.py index 02621d14a86f..4dc9acbb3dfe 100644 --- a/sorts/adaptive_merge_sort.py +++ b/sorts/adaptive_merge_sort.py @@ -1,3 +1,61 @@ +def adaptive_merge_sort(sequence: list) -> list: + """ + Sorts a list using the Adaptive Merge Sort algorithm. + + >>> adaptive_merge_sort([4, 3, 1, 2]) + Initial sequence: [4, 3, 1, 2] + Sorting: array[0:2] and array[2:4] + Sorting: array[0:1] and array[1:2] + Merging: array[0:1] and array[1:2] + After merge: [3, 4] + Sorting: array[2:3] and array[3:4] + Skipping merge as array[2] <= array[3] + Merging: array[0:2] and array[2:4] + After merge: [1, 2, 3, 4] + Sorted sequence: [1, 2, 3, 4] + [1, 2, 3, 4] + """ + if len(sequence) < 2: + return sequence + + aux = sequence[:] + print(f"Initial sequence: {sequence}") + adaptive_merge_sort_helper(sequence, aux, 0, len(sequence) - 1) + print(f"Sorted sequence: {sequence}") + return sequence + + +def adaptive_merge_sort_helper(array: list, aux: list, low: int, high: int) -> None: + """ + Helper function for Adaptive Merge Sort algorithm. + + >>> adaptive_merge_sort_helper([4, 3, 1, 2], [4, 3, 1, 2], 0, 3) + Sorting: array[0:2] and array[2:4] + Sorting: array[0:1] and array[1:2] + Merging: array[0:1] and array[1:2] + After merge: [3, 4] + Sorting: array[2:3] and array[3:4] + Skipping merge as array[2] <= array[3] + Merging: array[0:2] and array[2:4] + After merge: [1, 2, 3, 4] + """ + if high <= low: + return + + mid = (low + high) // 2 + print(f"Sorting: array[{low}:{mid + 1}] and array[{mid + 1}:{high + 1}]") + + adaptive_merge_sort_helper(aux, array, low, mid) + adaptive_merge_sort_helper(aux, array, mid + 1, high) + + if array[mid] <= array[mid + 1]: + print(f"Skipping merge as array[{mid}] <= array[{mid + 1}]") + array[low:high + 1] = aux[low:high + 1] + return + + merge(array, aux, low, mid, high) + + def merge(array: list, aux: list, low: int, mid: int, high: int) -> None: """ Merges two sorted subarrays of the main array. @@ -27,3 +85,8 @@ def merge(array: list, aux: list, low: int, mid: int, high: int) -> None: array[k] = aux[k] print(f"After merge: {array[low:high + 1]}") + + +# Example usage +if __name__ == "__main__": + print(adaptive_merge_sort([4, 3, 1, 2])) From 723ab7faf0c5279c1df29f0420fb212a3ff6a58a Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 16 Oct 2024 20:23:59 +0000 Subject: [PATCH 23/24] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- sorts/adaptive_merge_sort.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sorts/adaptive_merge_sort.py b/sorts/adaptive_merge_sort.py index 4dc9acbb3dfe..4729898f4338 100644 --- a/sorts/adaptive_merge_sort.py +++ b/sorts/adaptive_merge_sort.py @@ -50,7 +50,7 @@ def adaptive_merge_sort_helper(array: list, aux: list, low: int, high: int) -> N if array[mid] <= array[mid + 1]: print(f"Skipping merge as array[{mid}] <= array[{mid + 1}]") - array[low:high + 1] = aux[low:high + 1] + array[low : high + 1] = aux[low : high + 1] return merge(array, aux, low, mid, high) From 0eaeffbcf852fdae002aeac5395ecc0e4b7bf467 Mon Sep 17 00:00:00 2001 From: ANANT JAIN <139585700+anant-jain01@users.noreply.github.com> Date: Thu, 17 Oct 2024 12:21:53 +0530 Subject: [PATCH 24/24] Update adaptive_merge_sort.py --- sorts/adaptive_merge_sort.py | 49 ++++++------------------------------ 1 file changed, 7 insertions(+), 42 deletions(-) diff --git a/sorts/adaptive_merge_sort.py b/sorts/adaptive_merge_sort.py index 4729898f4338..2df5c3924a8d 100644 --- a/sorts/adaptive_merge_sort.py +++ b/sorts/adaptive_merge_sort.py @@ -1,20 +1,4 @@ def adaptive_merge_sort(sequence: list) -> list: - """ - Sorts a list using the Adaptive Merge Sort algorithm. - - >>> adaptive_merge_sort([4, 3, 1, 2]) - Initial sequence: [4, 3, 1, 2] - Sorting: array[0:2] and array[2:4] - Sorting: array[0:1] and array[1:2] - Merging: array[0:1] and array[1:2] - After merge: [3, 4] - Sorting: array[2:3] and array[3:4] - Skipping merge as array[2] <= array[3] - Merging: array[0:2] and array[2:4] - After merge: [1, 2, 3, 4] - Sorted sequence: [1, 2, 3, 4] - [1, 2, 3, 4] - """ if len(sequence) < 2: return sequence @@ -26,19 +10,6 @@ def adaptive_merge_sort(sequence: list) -> list: def adaptive_merge_sort_helper(array: list, aux: list, low: int, high: int) -> None: - """ - Helper function for Adaptive Merge Sort algorithm. - - >>> adaptive_merge_sort_helper([4, 3, 1, 2], [4, 3, 1, 2], 0, 3) - Sorting: array[0:2] and array[2:4] - Sorting: array[0:1] and array[1:2] - Merging: array[0:1] and array[1:2] - After merge: [3, 4] - Sorting: array[2:3] and array[3:4] - Skipping merge as array[2] <= array[3] - Merging: array[0:2] and array[2:4] - After merge: [1, 2, 3, 4] - """ if high <= low: return @@ -57,23 +28,17 @@ def adaptive_merge_sort_helper(array: list, aux: list, low: int, high: int) -> N def merge(array: list, aux: list, low: int, mid: int, high: int) -> None: - """ - Merges two sorted subarrays of the main array. - - >>> merge([4, 3, 1, 2], [4, 3, 1, 2], 0, 1, 3) - Merging: array[0:2] and array[2:4] - After merge: [1, 2, 3, 4] - """ print(f"Merging: array[{low}:{mid + 1}] and array[{mid + 1}:{high + 1}]") i, j = low, mid + 1 for k in range(low, high + 1): - if i > mid: - aux[k] = array[j] - j += 1 - elif j > high: - aux[k] = array[i] - i += 1 + if i > mid or j > high: + if i > mid: + aux[k] = array[j] + j += 1 + else: + aux[k] = array[i] + i += 1 elif array[i] <= array[j]: aux[k] = array[i] i += 1