padding
1
4
7
0
0
0
[0][1][2][3][4][5]
nums2
2
3
6
[0][1][2]
Brute force · concatenate + sort
▸1# copy nums2 into the tail of nums12for j ← 0 to n − 1: nums1[m + j] ← nums2[j]3sort(nums1)
state
- m3
- n3
nums1 holds m sorted values followed by n zeros of padding; nums2 holds n sorted values. Merge nums2 into nums1 in place so nums1 ends up sorted.
▸1# copy nums2 into the tail of nums12for j ← 0 to n − 1: nums1[m + j] ← nums2[j]3sort(nums1)
line 1Drop nums2 into the padding at the end of nums1, then sort the whole thing.