I have lArr
(left) {1,2,4,5}
and rArr
(right) {6,8,10,13}
, I want to merge them into one sorted array, but my code is not functioning how I want it to.
private static ArrayList merge(int[] lArr, int[] rArr) {
ArrayList mergedArray = new ArrayList();
int i = 0;
int j = 0;
while (i < lArr.length && j < rArr.length) {
if (lArr[i] < rArr[j]) {
mergedArray.add(lArr[i]);
i++;
}
// so up until here, the code runs,
// but it never reaches the else segment.
else {
mergedArray.add(rArr[j]);
j++;
}
}
return mergedArray;
}
after calling the merge()
method from the main, lArr(left array)
is only displayed.