예전에 합병정렬을 구현해볼 때처럼 각각의 배열에 전용 인덱스를 0부터 두고 오름차순으로 정렬을 시킬때 비교를 해서 작은 배열의 idx만 증가하는 방식으로 구현을 했다.
while문의 조건은 어느 한 인덱스가 배열 범위를 벗어나면 while문을 빠져나오는데, 이때 다른 한 배열의 인덱스는 배열의 끝까지 못간 상태로 빠져나온다.
그래서 추가적인 코드를 작성하여 나머지 부분을 마저 출력해주도록 했다.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
ios::sync_with_stdio(0);
cout.tie(0);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<int> a(n);
vector<int> b(m);
int i;
for (i = 0; i < n; i++)
cin >> a[i];
for (i = 0; i < m; i++)
cin >> b[i];
int aIdx=0, bIdx=0;
while(aIdx < n && bIdx < m)
{
if (a[aIdx] <= b[bIdx])
cout << a[aIdx++] << ' ';
else
cout << b[bIdx++] << ' ';
}
while (aIdx < n)
cout << a[aIdx++] << ' ';
while (bIdx < m)
cout << b[bIdx++] << ' ';
return 0;
}