-
Notifications
You must be signed in to change notification settings - Fork 0
/
MergeSortedArray.py
31 lines (24 loc) · 971 Bytes
/
MergeSortedArray.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
import TestHelper
def MergeSortedArrays(a, b):
# define index for each array
indexA = indexB = 0
# store each array length in variable
lenA = len(a)
lenB = len(b)
# define empty array
result = []
# loop until the 2 indices are larger than or equal their array length
while indexA < lenA or indexB < lenB:
# loop on the 1st array until you found element > current element of 2nd array
if indexA < lenA and (indexB >= lenB or a[indexA] <= b[indexB]):
result.append(a[indexA])
indexA += 1
# loop on the 2nd array until you found element > current element of 1st array
if indexB < lenB and (indexA >= lenA or b[indexB] <= a[indexA]):
result.append(b[indexB])
indexB += 1
return result
a = TestHelper.GenerateRandomIntSortedList(4, 0, 1000)
b = TestHelper.GenerateRandomIntSortedList(3, 0, 1000)
result = MergeSortedArrays(a, b)
print(a, b, result)