-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathmerge_sorted_array.py
More file actions
46 lines (38 loc) · 1.16 KB
/
merge_sorted_array.py
File metadata and controls
46 lines (38 loc) · 1.16 KB
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
# Code to merge two different sorted arrays
input_str1= input('Enter elements of the first array separated by space: ')
print("\n")
arr1 = input_str1.split() # to convert the input string to list
print('array1: ', arr1)
input_str2= input('Enter elements of the second array separated by space: ')
print("\n")
arr2 = input_str2.split() # to convert the input string to list
print('array2: ', arr2)
for i in range(len(arr1)): # to convert each element to integer type
arr1[i] = int(arr1[i])
for i in range(len(arr2)): # to convert each element to integer type
arr2[i] = int(arr2[i])
def mergeArrays(arr1, arr2):
n1 = len(arr1)
n2 = len(arr2)
arr3 = [None] * (n1 + n2)
i,j,k = 0,0,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
for i in range(n1 + n2):
print(str(arr3[i]), end = " ")
mergeArrays(arr1, arr2);