-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmerge_sort.py
More file actions
35 lines (31 loc) · 764 Bytes
/
merge_sort.py
File metadata and controls
35 lines (31 loc) · 764 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
29
30
31
32
33
34
35
### Divide and Conquer Example ###
### MergeSort: Order( nlog(n) )
#Helper Function: merge, for mergeSort
def merge(A, B):
out = []
i,j=0,0
while i < len(A) and j < len(B):
if A[i] < B[j]:
out.append(A[i])
i+=1
else:
out.append(B[j])
j+=1
while i < len(A):
out.append(A[i])
i+=1
while j < len(B):
out.append(B[j])
j+=1
return out
def mergeSort(L):
print(f"Before the Sort: {L}")
if len(L) < 2:
return L[:]
else:
mid = len(L)//2
Left = mergeSort(L[:mid])
Right = mergeSort(L[mid:])
return merge(Left,Right)
L = [1,7,4,2,55,89898,0]
print(mergeSort(L))