Page 1 of 1

merging 2 arrays using recursion

Posted: Thu Mar 01, 2018 4:39 am
by maty.tsoraro@gmail.com
Hello,
i have a recursive method int[]merge(int[]ar1,int[]ar2)
that merges 2 arrays (that are already sorted from small to large) to a 3rd array names res.
trying to excute it in my program with no luck
please help
this is the URL link
http://www.beta.browxy.com#USER_159157
and this is the program below..

Code: Select all

public class MergeArray{
    
    public static void main(String[] args){
    int[]ar1 = {5,6,7,8,9};
    int[]ar2 = {10,11,12,13,14,15,16,17};
    int[]merge(int[]ar1,int[]ar2);
        
    }
    public static int[]merge(int[]ar1,int[]ar2){
        int[]res = new int(ar1.length+ar2.length]);
        return merge(ar1,0,ar2,0,res);
    }
    private static int[] merge(int[] ar1,int ix1,int[]ar2,int ix2,int[]res){
        if(ix1 == ar1.length && ix2 == ar2.length)//finish both arrays
        return res;
        if(ix2 == ar2.length || (ix1<ar1.length&&ar1[ix1]<ar2[ix2]))
        {
            res[ix1+ix2]=ar1[ix1];
            ix1 = ix1+1
        }else{
            res[ix1+ix2] = ar2[ix2];
        }
        return merge(ar1,ix1,ar2,ix2,res);
    }
}

Re: merging 2 arrays using recursion

Posted: Thu Mar 01, 2018 7:01 pm
by dbremmen@gmail.com
Hi!

Nice to know that you are learning recursion :)
Thanks for using the code tags and suggestions to post :D
About your code, indentation is important for better understanding the problem. I reformatted your code using a nice indentation and fixed a bug. Now I think is working.

URL: http://www.beta.browxy.com#ALIEN_11105

Result Code

Code: Select all

public class MergeArray{
    
    public static void main(String[] args) {
      int[] ar1 = {5,6,7,8,9};
      int[] ar2 = {10,11,12,13,14,15,16,17};
      int[] result = merge(ar1,ar2);
      for (int i=0; i<result.length; i++) {
        System.out.println(result[i]);
      }    
    }
    
    public static int[] merge(int[]ar1,int[]ar2) {
        int[] res = new int[ar1.length+ar2.length];
        return merge(ar1,0,ar2,0,res);
    }
    
    private static int[] merge(int[] ar1, int ix1, int[]ar2, int ix2, int[]res) {
        if (ix1 == ar1.length && ix2 == ar2.length) {
          return res;
        }
        if (ix2 == ar2.length || (ix1 < ar1.length && ar1[ix1] < ar2[ix2])) {
            res[ix1+ix2]=ar1[ix1];
            ix1 = ix1+1;
        } else {
            res[ix1+ix2] = ar2[ix2];
            ix2 = ix2+1;
        }
        return merge(ar1,ix1,ar2,ix2,res);
    }
}

Re: merging 2 arrays using recursion

Posted: Thu Mar 01, 2018 11:08 pm
by maty.tsoraro@gmail.com
hi,
yes it works :)
is there a way we excute it without a for loop in the main ?

Re: merging 2 arrays using recursion

Posted: Fri Mar 02, 2018 11:13 pm
by dbremmen@gmail.com
Hi!

The loop in the main is to print the result. The reason is that to print the values in an array you need to iterate it. One alternative is to create a new method called printResult and move the loop inside that method. Then in the main you can call that mehod

Re: merging 2 arrays using recursion

Posted: Sat Mar 03, 2018 4:36 pm
by maty.tsoraro@gmail.com
ok
thanks :)