MCPcopy Create free account
hub / github.com/Jack-Lee-Hiter/AlgorithmsByPython / mergeSort

Function mergeSort

MergeSort.py:1–27  ·  view source on GitHub ↗
(alist)

Source from the content-addressed store, hash-verified

1def mergeSort(alist):
2 if len(alist) > 1:
3 mid = len(alist)//2
4 lefthalf = alist[:mid]
5 righthalf = alist[mid:]
6
7 mergeSort(lefthalf)
8 mergeSort(righthalf)
9
10 i = 0; j = 0; k = 0
11 while i < len(lefthalf) and j < len(righthalf):
12 if lefthalf[i] < righthalf[j]:
13 alist[k] = lefthalf[i]
14 i += 1
15 else:
16 alist[k] = righthalf[j]
17 j += 1
18 k += 1
19
20 while i < len(lefthalf):
21 alist[k] = lefthalf[i]
22 i += 1
23 k += 1
24 while j < len(righthalf):
25 alist[k] = righthalf[j]
26 j += 1
27 k += 1
28
29alist = [54,26,93,17,77,31,44,55,20]
30mergeSort(alist)

Callers 1

MergeSort.pyFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected