10
10
python adaptive_merge_sort.py
11
11
"""
12
12
13
+
13
14
def adaptive_merge_sort (collection : list ) -> list :
14
15
"""
15
16
Sorts a list using an adaptive merge sort algorithm.
@@ -32,13 +33,16 @@ def adaptive_merge_sort(collection: list) -> list:
32
33
def find_run (collection : list , start : int ) -> int :
33
34
"""
34
35
Detects and returns the length of a naturally occurring run starting from 'start'.
35
-
36
+
36
37
:param collection: The list to detect runs in.
37
38
:param start: The starting index for finding the run.
38
39
:return: Length of the detected run.
39
40
"""
40
41
run_length = 1
41
- while start + run_length < len (collection ) and collection [start + run_length - 1 ] <= collection [start + run_length ]:
42
+ while (
43
+ start + run_length < len (collection )
44
+ and collection [start + run_length - 1 ] <= collection [start + run_length ]
45
+ ):
42
46
run_length += 1
43
47
return run_length
44
48
@@ -65,7 +69,7 @@ def merge(left: list, right: list) -> list:
65
69
# Step 1: Identify naturally occurring runs and store them in 'runs'
66
70
while i < len (collection ):
67
71
run_length = find_run (collection , i )
68
- runs .append (collection [i : i + run_length ])
72
+ runs .append (collection [i : i + run_length ])
69
73
i += run_length
70
74
71
75
# Step 2: Iteratively merge runs until one sorted collection remains
@@ -83,6 +87,7 @@ def merge(left: list, right: list) -> list:
83
87
84
88
if __name__ == "__main__" :
85
89
import doctest
90
+
86
91
doctest .testmod ()
87
92
try :
88
93
user_input = input ("Enter numbers separated by a comma:\n " ).strip ()
0 commit comments