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

Function merge

Sorting Algorithms/Merge-sort.py:12–37  ·  view source on GitHub ↗
(ori_lst, left, mid, right)

Source from the content-addressed store, hash-verified

10
11
12def merge(ori_lst, left, mid, right):
13 L, R = [], [] # PREPARE TWO TEMPORARY LIST TO HOLD ELEMENTS
14 for i in range(left, mid): # LOADING
15 L.append(ori_lst[i])
16 for i in range(mid, right): # LOADING
17 R.append(ori_lst[i])
18 base = left # FILL ELEMENTS BACK TO ORIGINAL LIST START FROM INDEX LEFT
19 # EVERY LOOP CHOOSE A SMALLER ELEMENT FROM EITHER LIST
20 while len(L) > 0 and len(R) > 0:
21 if L[0] < R[0]:
22 ori_lst[base] = L[0]
23 L.remove(L[0])
24 else:
25 ori_lst[base] = R[0]
26 R.remove(R[0])
27 base += 1
28 # UNLOAD THE REMAINER
29 while len(L) > 0:
30 ori_lst[base] = L[0]
31 L.remove(L[0])
32 base += 1
33 while len(R) > 0:
34 ori_lst[base] = R[0]
35 R.remove(R[0])
36 base += 1
37 # ORIGINAL LIST SHOULD BE SORTED FROM INDEX LEFT TO INDEX RIGHT
38
39
40def merge_sort(L, left, right):

Callers 1

merge_sortFunction · 0.70

Calls 2

appendMethod · 0.45
removeMethod · 0.45

Tested by

no test coverage detected