mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-02-07 01:50:55 +00:00
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
This commit is contained in:
parent
c8397d47cf
commit
ddfcaecb77
|
@ -17,7 +17,7 @@
|
||||||
"""
|
"""
|
||||||
if len(sequence) < 2:
|
if len(sequence) < 2:
|
||||||
return sequence
|
return sequence
|
||||||
|
|
||||||
aux = sequence[:]
|
aux = sequence[:]
|
||||||
print(f"Initial sequence: {sequence}")
|
print(f"Initial sequence: {sequence}")
|
||||||
adaptive_merge_sort_helper(sequence, aux, 0, len(sequence) - 1)
|
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:
|
if high <= low:
|
||||||
return
|
return
|
||||||
|
|
||||||
mid = (low + high) // 2
|
mid = (low + high) // 2
|
||||||
print(f"Sorting: array[{low}:{mid + 1}] and array[{mid + 1}:{high + 1}]")
|
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, low, mid)
|
||||||
adaptive_merge_sort_helper(aux, array, mid + 1, high)
|
adaptive_merge_sort_helper(aux, array, mid + 1, high)
|
||||||
|
|
||||||
if array[mid] <= array[mid + 1]:
|
if array[mid] <= array[mid + 1]:
|
||||||
print(f"Skipping merge as 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
|
return
|
||||||
|
|
||||||
merge(array, aux, low, mid, high)
|
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]
|
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:
|
||||||
|
@ -80,10 +80,10 @@ def merge(array: list, aux: list, low: int, mid: int, high: int) -> None:
|
||||||
else:
|
else:
|
||||||
aux[k] = array[j]
|
aux[k] = array[j]
|
||||||
j += 1
|
j += 1
|
||||||
|
|
||||||
for k in range(low, high + 1):
|
for k in range(low, high + 1):
|
||||||
array[k] = aux[k]
|
array[k] = aux[k]
|
||||||
|
|
||||||
print(f"After merge: {array[low:high + 1]}")
|
print(f"After merge: {array[low:high + 1]}")
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user