-
Notifications
You must be signed in to change notification settings - Fork 0
/
mergesort.py
62 lines (54 loc) · 1.46 KB
/
mergesort.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
"""
Merge Sort
"""
def mergesort_routine(A,l,u):
if l >= u:
return
m = (l+u)/2 #flooring
mergesort_routine(A,0,m) #sorting A[l:m+1]
mergesort_routine(A,m+1,u) #sorting A[m+1:u+1]
#merge the sorted arrays A[l:m+1] and A[m+1:u+1]
L = [x for x in A[l:m+1]]
##print "L is ",L
#U = A[m+1:u+1]
U = [x for x in A[m+1:u+1]]
##print "U is ",U
n1 = len(L)
n2 = len(U)
# i is the number of the elements copied from L
# j is the number of the elements copied from U
# Loop invariant: A[0:i+j] is a sorted array comprising of elements from L[0:i] and U[0:j]
# where i in [0:n1] and j in [0:n2]
# i(j) is the number of the elements copied from L(U)
# Bound function: i+j
i = 0
j = 0
B = []
while i<n1 and j<n2:
if L[i] <= U[j]:
B.append(L[i])
i = i+1
else:
B.append(U[j])
j = j+1
if i == n1:
while j<n2:
B.append(U[j])
j = j+1
elif j == n2:
while i<n1:
B.append(L[i])
i = i+1
#return A
A[l:u+1] = B
print "The array is ", B
def mergesort(A):
#if not is type(A,list):
#print "Error: "
#return
N = len(A)
mergesort_routine(A,0,N-1)
print " Enter the array of integers : "
A = [int(x) for x in raw_input().split()]
#print "the array before sorting ",A
mergesort(A)