You need to go from l
to m
when filling vector L
:
void merge(vector<int>& a, int l, int m, int r) {
vector<int> L;
vector<int> R;
for (int i = l; i <= m; i++)
L.push_back(a[i]);
...
}
Otherwise L
contains additional elements outside the range of interest. Then, index k
steps off the end of vector a
when iterating for the size of L
.
2
solved Passing vector by reference: Segmentation fault [duplicate]