-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_sort.py
More file actions
48 lines (39 loc) · 771 Bytes
/
python_sort.py
File metadata and controls
48 lines (39 loc) · 771 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
36
37
38
39
40
41
42
43
44
45
'''
merge sort
MergeSort(arr[], l, r)
if r > l
1. find the middle point to divide the array into two halves:
middle m = l + (r-1)/2
2. call mergesort for first half
mergesort(arr, l, m)
3. call mergsort for second half
mergesort(arr, m+1, r)
4. merge the two halves sorted in step 2 and 3
merge(arr, l, m, r)
'''
def mergeSort(arr):
if len(arr) > 1:
mid = len(arr)//2
larr = arr[:mid]
rarr = arr[mid:]
mergeSort(larr)
mergeSort(rarr)
i = j = k = 0
# merge
while i<len(larr) and j<len(rarr):
if larr[i] < rarr[j]:
arr[k] = larr[i]
i+=1
else:
arr[k] = rarr[j]
j+=1
k += 1
#left part, just copy
while i<len(larr):
arr[k] = larr[i]
i+=1
k+=1
while j<len(rarr):
arr[k] = rarr[j]
j+=1
k+=1