-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmerge-sorted-arrays.py
More file actions
28 lines (19 loc) · 857 Bytes
/
merge-sorted-arrays.py
File metadata and controls
28 lines (19 loc) · 857 Bytes
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
def merge_sorted_lists(lst1, lst2):
"""Takes two sorted arrays and returns a single sorted array."""
merged_list_size = len(lst1) + len(lst2)
merged_list = [None] * merged_list_size
current_index_lst1 = 0
current_index_lst2 = 0
current_index_merged = 0
while current_index_merged < merged_list_size:
if current_index_lst1 >= len(current_index_lst1):
first_unmerged_lst1 = lst1[current_index_lst1]
first_unmerged_lst2 = lst2[current_index_lst2]
if first_unmerged_lst1 < first_unmerged_lst2:
merged_list[current_index_merged] = first_unmerged_lst1
current_index_lst1 += 1
else:
merged_list[current_index_merged] = first_unmerged_lst2
current_index_lst2 += 1
current_index_merged += 1
return merged_list