@@ -11,6 +11,10 @@ def merge(arr: list[int]) -> list[int]:
11
11
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
12
12
>>> merge([10,22,1,2,3,9,15,23])
13
13
[1, 2, 3, 9, 10, 15, 22, 23]
14
+ >>> merge([100])
15
+ [100]
16
+ >>> merge([])
17
+ []
14
18
"""
15
19
if len (arr ) > 1 :
16
20
middle_length = len (arr ) // 2 ##Finds the middle of the array
@@ -24,30 +28,30 @@ def merge(arr: list[int]) -> list[int]:
24
28
right_size = len (right_array )
25
29
merge (left_array ) ## Starts sorting the left.
26
30
merge (right_array ) ##Starts sorting the right
27
- leftIndex = 0 ##Left Counter
28
- rightIndex = 0 ## Right Counter
31
+ left_index = 0 ##Left Counter
32
+ right_index = 0 ## Right Counter
29
33
index = 0 ## Position Counter
30
34
while (
31
- leftIndex < left_size and rightIndex < right_size
35
+ left_index < left_size and right_index < right_size
32
36
): ##Runs until the lowers size of the left and right are sorted.
33
- if left_array [leftIndex ] < right_array [rightIndex ]:
34
- arr [index ] = left_array [leftIndex ]
35
- leftIndex = leftIndex + 1
37
+ if left_array [left_index ] < right_array [right_index ]:
38
+ arr [index ] = left_array [left_index ]
39
+ left_index = left_index + 1
36
40
else :
37
- arr [index ] = right_array [rightIndex ]
38
- rightIndex = rightIndex + 1
41
+ arr [index ] = right_array [right_index ]
42
+ right_index = right_index + 1
39
43
index = index + 1
40
44
while (
41
- leftIndex < left_size
45
+ left_index < left_size
42
46
): ##Adds the left over elements in the left half of the array
43
- arr [index ] = left_array [leftIndex ]
44
- leftIndex = leftIndex + 1
47
+ arr [index ] = left_array [left_index ]
48
+ left_index = left_index + 1
45
49
index = index + 1
46
50
while (
47
- rightIndex < right_size
51
+ right_index < right_size
48
52
): ##Adds the left over elements in the right half of the array
49
- arr [index ] = right_array [rightIndex ]
50
- rightIndex = rightIndex + 1
53
+ arr [index ] = right_array [right_index ]
54
+ right_index = right_index + 1
51
55
index = index + 1
52
56
return arr
53
57
0 commit comments