Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions sorts/adaptive_merge_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
"""
Adaptive Merge Sort Algorithm
@see https://www.tutorialspoint.com/adaptive-merging-and-sorting-in-data-structure
"""

def adaptive_merge_sort(sequence: list) -> list:
"""
Sorts a list using the Adaptive Merge Sort algorithm.

:param sequence: list of elements to be sorted
:return: sorted list

>>> adaptive_merge_sort([12, 11, 13, 5, 6, 7])
[5, 6, 7, 11, 12, 13]

>>> adaptive_merge_sort([5, 4, 3, 2, 1])
[1, 2, 3, 4, 5]

>>> adaptive_merge_sort(['zebra', 'apple', 'mango', 'banana'])
['apple', 'banana', 'mango', 'zebra']
"""
if len(sequence) < 2:
return sequence

aux = sequence[:]
adaptive_merge_sort_helper(sequence, aux, 0, len(sequence) - 1)
return sequence


def adaptive_merge_sort_helper(array: list, aux: list, low: int, high: int):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: adaptive_merge_sort_helper. If the function does not return a value, please provide the type hint as: def function() -> None:

As there is no test file in this pull request nor any test function or class in the file sorts/adaptive_merge_sort.py, please provide doctest for the function adaptive_merge_sort_helper

if high <= low:
return

mid = (low + high) // 2

adaptive_merge_sort_helper(aux, array, low, mid)
adaptive_merge_sort_helper(aux, array, mid + 1, high)

if array[mid] <= array[mid + 1]:
array[low:high + 1] = aux[low:high + 1]
return

merge(array, aux, low, mid, high)


def merge(array: list, aux: list, low: int, mid: int, high: int):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: merge. If the function does not return a value, please provide the type hint as: def function() -> None:

As there is no test file in this pull request nor any test function or class in the file sorts/adaptive_merge_sort.py, please provide doctest for the function merge

i, j = low, mid + 1

for k in range(low, high + 1):
if i > mid:
aux[k] = array[j]
j += 1
elif j > high:
aux[k] = array[i]
i += 1
elif array[j] < array[i]:
aux[k] = array[j]
j += 1
else:
aux[k] = array[i]
i += 1
Loading