-
Notifications
You must be signed in to change notification settings - Fork 0
/
15_merge_two_array_and_sort.py
47 lines (47 loc) · 1.18 KB
/
15_merge_two_array_and_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
35
36
37
38
39
40
41
42
43
44
45
46
47
# Python program to merge two unsorted lists
# in sorted order
# Function to merge array in sorted order
def unsortedarray (a, b, res, n, m):
# Sorting a[] and b[]
a.sort()
b.sort()
# Merge two sorted arrays into res[]
i, j, k = 0, 0, 0
while (i < n and j < m):
if (a[i] <= b[j]):
res[k] = a[i]
i += 1
k += 1
else:
res[k] = b[j]
j += 1
k += 1
while (i < n): # Merging remaining
# elements of a[] (if any)
res[k] = a[i]
i += 1
k += 1
while (j < m): # Merging remaining
# elements of b[] (if any)
res[k] = b[j]
j += 1
k += 1
# Driver code
A=list()
n=int(input("Enter the size of the First List ::"))
print("Enter the Element of First List ::")
for i in range(int(n)):
k=int(input(""))
A.append(k)
B=list()
m=int(input("Enter the size of the Second List ::"))
print("Enter the Element of Second List ::")
for i in range(int(n)):
k=int(input(""))
B.append(k)
# Final merge list
res = [0 for i in range(n + m)]
unsortedarray(A, B, res, n, m)
print ("Sorted merged list :")
for i in range(n + m):
print (res[i],)