# merge sorted array

```
1def 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:

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

Output:

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