MCPcopy Create free account
hub / github.com/geekcomputers/Python / merge

Function merge

Sorting Algorithms/merge_sort.py:1–33  ·  view source on GitHub ↗
(left_list, right_list)

Source from the content-addressed store, hash-verified

1def merge(left_list, right_list):
2 sorted_list = []
3 left_list_index = right_list_index = 0
4
5 # We use the list lengths often, so its handy to make variables
6 left_list_length, right_list_length = len(left_list), len(right_list)
7
8 for _ in range(left_list_length + right_list_length):
9 if left_list_index < left_list_length and right_list_index < right_list_length:
10 # We check which value from the start of each list is smaller
11 # If the item at the beginning of the left list is smaller, add it
12 # to the sorted list
13 if left_list[left_list_index] <= right_list[right_list_index]:
14 sorted_list.append(left_list[left_list_index])
15 left_list_index += 1
16 # If the item at the beginning of the right list is smaller, add it
17 # to the sorted list
18 else:
19 sorted_list.append(right_list[right_list_index])
20 right_list_index += 1
21
22 # If we've reached the end of the of the left list, add the elements
23 # from the right list
24 elif left_list_index == left_list_length:
25 sorted_list.append(right_list[right_list_index])
26 right_list_index += 1
27 # If we've reached the end of the of the right list, add the elements
28 # from the left list
29 elif right_list_index == right_list_length:
30 sorted_list.append(left_list[left_list_index])
31 left_list_index += 1
32
33 return sorted_list
34
35
36def merge_sort(nums):

Callers 1

merge_sortFunction · 0.70

Calls 1

appendMethod · 0.45

Tested by

no test coverage detected