forked from parthbheda/pythoniffie
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmerge_two_sorted_arrays.py
More file actions
45 lines (33 loc) · 820 Bytes
/
merge_two_sorted_arrays.py
File metadata and controls
45 lines (33 loc) · 820 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
# Program to merge two sorted arrays
def mergeArrays(arr1, arr2, n1, n2):
arr3 = [None] * (n1 + n2)
i = 0
j = 0
k = 0
while i < n1 and j < n2:
if arr1[i] < arr2[j]:
arr3[k] = arr1[i]
k = k + 1
i = i + 1
else:
arr3[k] = arr2[j]
k = k + 1
j = j + 1
while i < n1:
arr3[k] = arr1[i];
k = k + 1
i = i + 1
while j < n2:
arr3[k] = arr2[j];
k = k + 1
j = j + 1
print("New array after merging the two arrays")
for i in range(n1 + n2):
print(arr3[i], end=' ')
print()
#Example input
arr1 = [1, 3, 5, 7]
n1 = len(arr1)
arr2 = [2, 4, 6, 8]
n2 = len(arr2)
mergeArrays(arr1, arr2, n1, n2);