-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
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
anant-jain01
wants to merge
24
commits into
TheAlgorithms:master
Choose a base branch
from
anant-jain01:patch-12
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+57
−0
Open
Changes from 15 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
451d4e1
Create adaptive_merge_sort.py
anant-jain01 97aa418
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 8d5ecd6
Update adaptive_merge_sort.py
anant-jain01 f046da9
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 94afed0
Update adaptive_merge_sort.py
anant-jain01 cd6fca8
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 4a0f090
Update adaptive_merge_sort.py
anant-jain01 b17141a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 513ed29
Update adaptive_merge_sort.py
anant-jain01 3cabfc6
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] cb4acf5
Update adaptive_merge_sort.py
anant-jain01 15578e8
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] f331afd
Update adaptive_merge_sort.py
anant-jain01 eb3284d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] c8397d4
Update adaptive_merge_sort.py
anant-jain01 ddfcaec
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] b08a35c
Update adaptive_merge_sort.py
anant-jain01 ad7fbab
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 952dc0a
Update adaptive_merge_sort.py
anant-jain01 7d3d131
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] b82503e
Update adaptive_merge_sort.py
anant-jain01 29f60d1
Update adaptive_merge_sort.py
anant-jain01 723ab7f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 0eaeffb
Update adaptive_merge_sort.py
anant-jain01 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
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: | ||
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: | ||
""" | ||
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: | ||
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: | ||
""" | ||
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}]") | ||
|
||
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[i] <= array[j]: # Keep stable by using <= | ||
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])) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An error occurred while parsing the file:
sorts/adaptive_merge_sort.py