Skip to content

Commit cb4acf5

Browse files
authored
Update adaptive_merge_sort.py
1 parent 3cabfc6 commit cb4acf5

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

sorts/adaptive_merge_sort.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ def adaptive_merge_sort(sequence: list) -> list:
66
Initial sequence: [4, 3, 1, 2]
77
Sorting: array[0:2] and array[2:4]
88
Sorting: array[0:1] and array[1:2]
9+
Merging: array[0:1] and array[1:2]
10+
After merge: [3, 4]
911
Sorting: array[2:3] and array[3:4]
1012
Skipping merge as array[0] <= array[1]
1113
Skipping merge as array[2] <= array[3]
@@ -22,16 +24,16 @@ def adaptive_merge_sort(sequence: list) -> list:
2224
print(f"Sorted sequence: {sequence}")
2325
return sequence
2426

25-
2627
def adaptive_merge_sort_helper(array: list, aux: list, low: int, high: int) -> None:
2728
"""
2829
Helper function for Adaptive Merge Sort algorithm.
2930
3031
>>> adaptive_merge_sort_helper([4, 3, 1, 2], [4, 3, 1, 2], 0, 3)
3132
Sorting: array[0:2] and array[2:4]
3233
Sorting: array[0:1] and array[1:2]
34+
Merging: array[0:1] and array[1:2]
35+
After merge: [3, 4]
3336
Sorting: array[2:3] and array[3:4]
34-
Skipping merge as array[0] <= array[1]
3537
Skipping merge as array[2] <= array[3]
3638
Merging: array[0:2] and array[2:4]
3739
After merge: [1, 2, 3, 4]
@@ -48,7 +50,6 @@ def adaptive_merge_sort_helper(array: list, aux: list, low: int, high: int) -> N
4850
return
4951
merge(array, aux, low, mid, high)
5052

51-
5253
def merge(array: list, aux: list, low: int, mid: int, high: int) -> None:
5354
"""
5455
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:
7273
else:
7374
aux[k] = array[i]
7475
i += 1
76+
array[low:high + 1] = aux[low:high + 1] # Update the main array with merged values
7577
print(f"After merge: {aux[low:high + 1]}")

0 commit comments

Comments
 (0)