Skip to content

Create adaptive_merge_sort.py #11993

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 24 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
451d4e1
Create adaptive_merge_sort.py
anant-jain01 Oct 11, 2024
97aa418
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 11, 2024
8d5ecd6
Update adaptive_merge_sort.py
anant-jain01 Oct 16, 2024
f046da9
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 16, 2024
94afed0
Update adaptive_merge_sort.py
anant-jain01 Oct 16, 2024
cd6fca8
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 16, 2024
4a0f090
Update adaptive_merge_sort.py
anant-jain01 Oct 16, 2024
b17141a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 16, 2024
513ed29
Update adaptive_merge_sort.py
anant-jain01 Oct 16, 2024
3cabfc6
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 16, 2024
cb4acf5
Update adaptive_merge_sort.py
anant-jain01 Oct 16, 2024
15578e8
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 16, 2024
f331afd
Update adaptive_merge_sort.py
anant-jain01 Oct 16, 2024
eb3284d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 16, 2024
c8397d4
Update adaptive_merge_sort.py
anant-jain01 Oct 16, 2024
ddfcaec
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 16, 2024
b08a35c
Update adaptive_merge_sort.py
anant-jain01 Oct 16, 2024
ad7fbab
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 16, 2024
952dc0a
Update adaptive_merge_sort.py
anant-jain01 Oct 16, 2024
7d3d131
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 16, 2024
b82503e
Update adaptive_merge_sort.py
anant-jain01 Oct 16, 2024
29f60d1
Update adaptive_merge_sort.py
anant-jain01 Oct 16, 2024
723ab7f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 16, 2024
0eaeffb
Update adaptive_merge_sort.py
anant-jain01 Oct 17, 2024
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
57 changes: 57 additions & 0 deletions sorts/adaptive_merge_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
def adaptive_merge_sort(sequence: list) -> list:
if len(sequence) < 2:
return sequence

aux = sequence[:]
print(f"Initial sequence: {sequence}")
adaptive_merge_sort_helper(sequence, aux, 0, len(sequence) - 1)
print(f"Sorted sequence: {sequence}")
return sequence


def adaptive_merge_sort_helper(array: list, aux: list, low: int, high: int) -> None:
if high <= low:
return

mid = (low + high) // 2
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, mid + 1, high)

if array[mid] <= array[mid + 1]:
print(f"Skipping merge as 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) -> None:
print(f"Merging: array[{low}:{mid + 1}] and array[{mid + 1}:{high + 1}]")

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

for k in range(low, high + 1):
array[k] = aux[k]

print(f"After merge: {array[low:high + 1]}")


# Example usage
if __name__ == "__main__":
print(adaptive_merge_sort([4, 3, 1, 2]))