Skip to content

Commit a020029

Browse files
Create adaptive_merge_sort.py (#11993)
* Create adaptive_merge_sort.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update adaptive_merge_sort.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update adaptive_merge_sort.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update adaptive_merge_sort.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update adaptive_merge_sort.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update adaptive_merge_sort.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update adaptive_merge_sort.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update adaptive_merge_sort.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update adaptive_merge_sort.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update adaptive_merge_sort.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update adaptive_merge_sort.py * Update adaptive_merge_sort.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update adaptive_merge_sort.py --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent cd94f1a commit a020029

1 file changed

Lines changed: 57 additions & 0 deletions

File tree

sorts/adaptive_merge_sort.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
def adaptive_merge_sort(sequence: list) -> list:
2+
if len(sequence) < 2:
3+
return sequence
4+
5+
aux = sequence[:]
6+
print(f"Initial sequence: {sequence}")
7+
adaptive_merge_sort_helper(sequence, aux, 0, len(sequence) - 1)
8+
print(f"Sorted sequence: {sequence}")
9+
return sequence
10+
11+
12+
def adaptive_merge_sort_helper(array: list, aux: list, low: int, high: int) -> None:
13+
if high <= low:
14+
return
15+
16+
mid = (low + high) // 2
17+
print(f"Sorting: array[{low}:{mid + 1}] and array[{mid + 1}:{high + 1}]")
18+
19+
adaptive_merge_sort_helper(aux, array, low, mid)
20+
adaptive_merge_sort_helper(aux, array, mid + 1, high)
21+
22+
if array[mid] <= array[mid + 1]:
23+
print(f"Skipping merge as array[{mid}] <= array[{mid + 1}]")
24+
array[low : high + 1] = aux[low : high + 1]
25+
return
26+
27+
merge(array, aux, low, mid, high)
28+
29+
30+
def merge(array: list, aux: list, low: int, mid: int, high: int) -> None:
31+
print(f"Merging: array[{low}:{mid + 1}] and array[{mid + 1}:{high + 1}]")
32+
33+
i, j = low, mid + 1
34+
for k in range(low, high + 1):
35+
if i > mid or j > high:
36+
if i > mid:
37+
aux[k] = array[j]
38+
j += 1
39+
else:
40+
aux[k] = array[i]
41+
i += 1
42+
elif array[i] <= array[j]:
43+
aux[k] = array[i]
44+
i += 1
45+
else:
46+
aux[k] = array[j]
47+
j += 1
48+
49+
for k in range(low, high + 1):
50+
array[k] = aux[k]
51+
52+
print(f"After merge: {array[low:high + 1]}")
53+
54+
55+
# Example usage
56+
if __name__ == "__main__":
57+
print(adaptive_merge_sort([4, 3, 1, 2]))

0 commit comments

Comments
 (0)