mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-01-30 14:13:44 +00:00
Update adaptive_merge_sort.py
This commit is contained in:
parent
723ab7faf0
commit
0eaeffbcf8
|
@ -1,20 +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.
|
|
||||||
|
|
||||||
>>> 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:
|
if len(sequence) < 2:
|
||||||
return sequence
|
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:
|
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:
|
if high <= low:
|
||||||
return
|
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:
|
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}]")
|
print(f"Merging: array[{low}:{mid + 1}] and array[{mid + 1}:{high + 1}]")
|
||||||
|
|
||||||
i, j = low, mid + 1
|
i, j = low, mid + 1
|
||||||
for k in range(low, high + 1):
|
for k in range(low, high + 1):
|
||||||
if i > mid:
|
if i > mid or j > high:
|
||||||
aux[k] = array[j]
|
if i > mid:
|
||||||
j += 1
|
aux[k] = array[j]
|
||||||
elif j > high:
|
j += 1
|
||||||
aux[k] = array[i]
|
else:
|
||||||
i += 1
|
aux[k] = array[i]
|
||||||
|
i += 1
|
||||||
elif array[i] <= array[j]:
|
elif array[i] <= array[j]:
|
||||||
aux[k] = array[i]
|
aux[k] = array[i]
|
||||||
i += 1
|
i += 1
|
||||||
|
|
Loading…
Reference in New Issue
Block a user