My recursion skips values while trying to combine arrays

Viewed 35

I am trying to combine and sort 2 arrays with recursion. Right know my code combines them and sorts it but it skips some values in the array. In this instance it skips 6 and 15. Somehow that values dont even get in the if statement when I print out all the values that pass through the statements. Does anyone have an idea what is going wrong?

 public static void main(String [] args)
    {
        findMe find = new findMe();
        int[] a = {1, 2, 3, 5, 8, 13, 21, 34, 55, 89};
        int[] b = {4, 6, 7, 9, 15, 22, 33, 39, 58, 91};
        int[] c = new int[20];
        int[] x = find.combineArrayV2(a, b, c,0 , 0, 0);
        for(int i = 0; i < x.length; i++){
            System.out.println("On position : " + i + " is value " + x[i]);
        }

    }
 public int[] combineArrayV2(int[] a, int[] b, int[] c, int i, int j, int x){

        if (i >= 10 && j >= 10) {
            return c;
        }
        if(i == 10){
            c[x] = b[j];
            System.out.println("On position " + j + " is " + b[j]);
            combineArrayV2(a, b, c, i, j + 1, x + 1);
        }
        if(j == 10){
            c[x] = a[i];
                System.out.println("On position " + i + " is " + a[i]);
            combineArrayV2(a, b, c, i + 1, j, x + 1);
        }
        else {
            if (a[i] < b[j]) {
                c[x] = a[i];
                System.out.println("On position " + i + " is " + a[i]);
                combineArrayV2(a, b, c, i + 1, j, x + 1);
            } else {
                c[x] = b[j];
            System.out.println("On position " + j + " is " + b[j]);
                combineArrayV2(a, b, c, i, j + 1 + 1, x + 1);
            }
        }
        return c;
    }
0 Answers
Related