-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerge_sort.py
34 lines (29 loc) · 878 Bytes
/
merge_sort.py
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
def mergeSort(a, temp, leftStart, rightEnd):
if(leftStart >= rightEnd):
return
middle = int((leftStart + rightEnd) / 2)
mergeSort(a, temp, leftStart, middle)
mergeSort(a, temp, middle + 1, rightEnd)
merge(a, temp, leftStart, middle, rightEnd)
return
def merge(a, temp, leftStart, middle, rightEnd):
left = leftStart
right = middle + 1
index = leftStart
while(left <= middle and right <= rightEnd):
if(a[left] <= a[right]):
temp[index] = a[left]
left += 1
else:
temp[index] = a[right]
right += 1
index += 1
while left <= middle:
temp[index] = a[left]
left += 1
index += 1
while right <= rightEnd:
temp[index] = a[right]
right += 1
index += 1
a[leftStart:rightEnd+1] = temp[leftStart:rightEnd+1]