-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
451d4e1
97aa418
8d5ecd6
f046da9
94afed0
cd6fca8
4a0f090
b17141a
513ed29
3cabfc6
cb4acf5
15578e8
f331afd
eb3284d
c8397d4
ddfcaec
b08a35c
ad7fbab
952dc0a
7d3d131
b82503e
29f60d1
723ab7f
0eaeffb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
def adaptive_merge_sort(sequence: list) -> list: | ||
""" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. An error occurred while parsing the file: Traceback (most recent call last):
File "/opt/render/project/src/algorithms_keeper/parser/python_parser.py", line 146, in parse
reports = lint_file(
^^^^^^^^^^
libcst._exceptions.ParserSyntaxError: Syntax Error @ 2:5.
parser error: error at 1:4: expected one of (, *, +, -, ..., AWAIT, EOF, False, NAME, NUMBER, None, True, [, break, continue, lambda, match, not, pass, ~
"""
^ |
||
Sorts a list using the Adaptive Merge Sort algorithm. | ||
""" | ||
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): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: As there is no test file in this pull request nor any test function or class in the file |
||
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): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: As there is no test file in this pull request nor any test function or class in the file |
||
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[j] < array[i]: | ||
aux[k] = array[j] | ||
j += 1 | ||
else: | ||
aux[k] = array[i] | ||
i += 1 | ||
|
||
print(f"After merge: {aux[low:high + 1]}") |
Uh oh!
There was an error while loading. Please reload this page.