Your algorithm looks basically correct. Without missing the exact code, I think that your problem is mainly that you have too many equalities, i.e. almost every comparison you have is equal to lessthanorequals, morethanorequals.
If a == b, then also a <= b and a> = b. Because of this, there are too many of your if statements. Keep track of what specific indexes should be, and limit your affairs to what they definitely should be.
I rewrote your code (without an editor, sorry, so it may not work out of the box), which should give you a good idea.
public class Two { public static void main(String[] args) { //sample problem int[] arrayA = {2,3,5,5,8,10,11,17,18,20}; int[] arrayB = {5,6,7,8,14,15,17}; final int sizeA = arrayA.length(); final int sizeB = arrayB.length(); final int sizeC = sizeA+sizeB; int[] arrayC = new int[sizeC]; int countA = 0; int countB = 0; int countC = 0; for (countC = 0; countC < sizeC; countC++) { // if a has run out, fill with b if (countA == sizeA && countB < sizeB) { arrayC[countC] = arrayB[countB]; countB++; } // if b has run out, fill with a else if (countA < sizeA && countB == sizeB) { arrayC[countC] = arrayA[countA]; countA++; } // countA < sizeA && countB < sizeB because // if countA == sizeA && countB == sizeB then also countC == sizeC // and the for-loop would have stopped. else { // mind, if arrayA[countA] == arrayB[countB] then first // a will be added and on the next pass b will be added if (arrayA[countA] <= arrayB[countB]) { arrayC[countC] = arrayA[countA]; countA++; } else { arrayC[countC] = arrayB[countB]; countB++; } } } } }
source share