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 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 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
92 changes: 92 additions & 0 deletions sorts/adaptive_merge_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
def adaptive_merge_sort(sequence: list) -> list:

Check failure on line 1 in sorts/adaptive_merge_sort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff

sorts/adaptive_merge_sort.py:1:1: SyntaxError: Unexpected indentation
"""

Check failure on line 2 in sorts/adaptive_merge_sort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff

sorts/adaptive_merge_sort.py:2:5: SyntaxError: Expected an indented block after function definition

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

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.

>>> 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

Check failure on line 20 in sorts/adaptive_merge_sort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

sorts/adaptive_merge_sort.py:20:1: W293 Blank line contains whitespace
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:

Check failure on line 28 in sorts/adaptive_merge_sort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff

sorts/adaptive_merge_sort.py:28:1: SyntaxError: Expected a statement
"""
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

Check failure on line 44 in sorts/adaptive_merge_sort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

sorts/adaptive_merge_sort.py:44:1: W293 Blank line contains whitespace
mid = (low + high) // 2
print(f"Sorting: array[{low}:{mid + 1}] and array[{mid + 1}:{high + 1}]")

Check failure on line 47 in sorts/adaptive_merge_sort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

sorts/adaptive_merge_sort.py:47:1: W293 Blank line contains whitespace
adaptive_merge_sort_helper(aux, array, low, mid)
adaptive_merge_sort_helper(aux, array, mid + 1, high)

Check failure on line 50 in sorts/adaptive_merge_sort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

sorts/adaptive_merge_sort.py:50:1: W293 Blank line contains whitespace
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

Check failure on line 55 in sorts/adaptive_merge_sort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

sorts/adaptive_merge_sort.py:55:1: W293 Blank line contains whitespace
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}]")

Check failure on line 68 in sorts/adaptive_merge_sort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

sorts/adaptive_merge_sort.py:68:1: W293 Blank line contains whitespace
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

Check failure on line 83 in sorts/adaptive_merge_sort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

sorts/adaptive_merge_sort.py:83:1: W293 Blank line contains whitespace
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]))
Loading