# merge sorted array

```
1 def merge_two_sorted_arrays(A, m, B, n):
2
3 a, b, write_idx = m - 1, n - 1, m + n - 1
4 while a >= 0 and b >= 0:
5 if A[a] > B[b]:
6 A[write_idx] = A[a]
7 a -= 1
8 else:
9 A[write_idx] = B[b]
10 b -= 1
11 write_idx -= 1
12 while b >= 0:
13 A[write_idx] = B[b]
14 write_idx, b = write_idx - 1, b - 1
```

Input:

```
1 A = [1,2,3,4,0,0,0]
2 B = [4,5,6]
3 a = 4
4 b = 3
```

Output:

```
1 A = [1,2,3,4,4,5,6]
2 B = [4,5,6]
3 a = 4
4 b = 3
```