Skip to content

Commit f331afd

Browse files
authored
Update adaptive_merge_sort.py
1 parent 15578e8 commit f331afd

File tree

1 file changed

+3
-6
lines changed

1 file changed

+3
-6
lines changed

sorts/adaptive_merge_sort.py

+3-6
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ def adaptive_merge_sort(sequence: list) -> list:
99
Merging: array[0:1] and array[1:2]
1010
After merge: [3, 4]
1111
Sorting: array[2:3] and array[3:4]
12-
Skipping merge as array[0] <= array[1]
1312
Skipping merge as array[2] <= array[3]
1413
Merging: array[0:2] and array[2:4]
1514
After merge: [1, 2, 3, 4]
@@ -24,7 +23,6 @@ def adaptive_merge_sort(sequence: list) -> list:
2423
print(f"Sorted sequence: {sequence}")
2524
return sequence
2625

27-
2826
def adaptive_merge_sort_helper(array: list, aux: list, low: int, high: int) -> None:
2927
"""
3028
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
5149
return
5250
merge(array, aux, low, mid, high)
5351

54-
5552
def merge(array: list, aux: list, low: int, mid: int, high: int) -> None:
5653
"""
5754
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:
7572
else:
7673
aux[k] = array[i]
7774
i += 1
78-
array[low : high + 1] = aux[
79-
low : high + 1
80-
] # Update the main array with merged values
75+
# Ensure we correctly copy back to the main array
76+
for k in range(low, high + 1):
77+
array[k] = aux[k]
8178
print(f"After merge: {aux[low:high + 1]}")

0 commit comments

Comments
 (0)