mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-02-07 01:50:55 +00:00
Update adaptive_merge_sort.py
This commit is contained in:
parent
3cabfc6082
commit
cb4acf52e3
|
@ -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]}")
|
||||
|
|
Loading…
Reference in New Issue
Block a user