-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge_iterative.py
153 lines (128 loc) · 3.47 KB
/
merge_iterative.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/usr/bin/python
import math
def insert_elements():
print "Do you want to enter numbers Manually(Yes - 1/ No- 0):?"
x=input()
a=[]
if(x==0):
a=[3,2,4,5,9,1,3]
elif(x==1):
print "please enter input: "
print "press -1 to exit "
x=input()
while(x!= -1):
a.append(x)
x=input()
else:
print "Not a avalid input"
return a
def append(a):
######################################################
#appending large numbers to the list to make it a power of 2
#####################################################
N=len(a)
y=0
if(N>0):
global y
y=float(math.log(N)/(math.log(2)))
y=math.ceil(y)
while((2**y) > N):
a.append(9999)
N=len(a)
return(a)
def merge(a):
global y
y=int(y)
print y
i=1
l=0
m=2**i
x=2**i
b=[]
N=len(a)
if(N==1):
return a
flag=True
for i in range (1,y+1):
#the sorted numbers are stored in list b
l=0
m=2**i
x=2**i
flag1=True
print"#######################################"
print "m & l :",(m,l)
if(flag==True):
print "the unsorted list at the stage ",i,"is:"
print a
while(flag==True and m<=N):
p=l
q=m-(x/2)
print "p & q",(p,q)
while((p<(m-(x/2))) and q< m):
#global b
if(a[p]<a[q]):
b.append(a[p])
p=p+1
else :
b.append(a[q])
q=q+1
while( q<m):
b.append(a[q])
q=q+1
while(p<(m-(x/2))):
b.append(a[p])
p=p+1
l=m
m=m+x
print b
flag1=False
flag=False
else:
#the sorted numbers are stored in list a
a=[]
print "the unsorted list at the stage ",i,"is:"
print b
if(flag1==True):
while(m<=N):
p=l
q=m-(x/2)
print "p & q",(p,q)
while((p<(m-(x/2))) and q< m):
if(b[p]<b[q]):
a.append(b[p])
p=p+1
else :
a.append(b[q])
q=q+1
while( q<m):
a.append(b[q])
q=q+1
while(p<(m-(x/2))):
a.append(b[p])
p=p+1
l=m
m=m+x
print a
b=[]
flag=True
if(i%2==0):
print "done"
return a
else :
print "done"
print i
print b
return b
if(__name__=='__main__'):
a=insert_elements()
N=len(a)
y=0
print "the input array is:"
print a
a=append(a)
print "The input array after appending some numbers"
print a
a=merge(a)
print"#############################"
print("sorted array is:")
print a