forked from keon/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmerge_sort.py
More file actions
64 lines (47 loc) · 1.67 KB
/
merge_sort.py
File metadata and controls
64 lines (47 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"""
Merge Sort
Merge sort divides the array in half, recursively sorts each half, and
then merges the two sorted halves back together.
Reference: https://en.wikipedia.org/wiki/Merge_sort
Complexity:
Time: O(n log n) best / O(n log n) average / O(n log n) worst
Space: O(n)
"""
from __future__ import annotations
def merge_sort(array: list[int]) -> list[int]:
"""Sort an array in ascending order using merge sort.
Args:
array: List of integers to sort.
Returns:
A sorted list.
Examples:
>>> merge_sort([3, 1, 2])
[1, 2, 3]
"""
if len(array) <= 1:
return array
mid = len(array) // 2
left = merge_sort(array[:mid])
right = merge_sort(array[mid:])
_merge(left, right, array)
return array
def _merge(left: list[int], right: list[int], merged: list[int]) -> None:
"""Merge two sorted lists into *merged* in-place.
Args:
left: Sorted left half.
right: Sorted right half.
merged: Destination list (length == len(left) + len(right)).
"""
left_cursor = 0
right_cursor = 0
while left_cursor < len(left) and right_cursor < len(right):
if left[left_cursor] <= right[right_cursor]:
merged[left_cursor + right_cursor] = left[left_cursor]
left_cursor += 1
else:
merged[left_cursor + right_cursor] = right[right_cursor]
right_cursor += 1
for left_cursor in range(left_cursor, len(left)): # noqa: B020
merged[left_cursor + right_cursor] = left[left_cursor]
for right_cursor in range(right_cursor, len(right)): # noqa: B020
merged[left_cursor + right_cursor] = right[right_cursor]