merging 2 arrays using recursion

Do you have a compilation error or a difficult problem to solve? Ask for help here
Post Reply
maty.tsoraro@gmail.com
Posts: 32
Joined: Thu Dec 21, 2017 10:06 pm

merging 2 arrays using recursion

Post by maty.tsoraro@gmail.com » Thu Mar 01, 2018 4:39 am

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);
    }
}

dbremmen@gmail.com
Posts: 63
Joined: Sun Jan 07, 2018 6:30 pm

Re: merging 2 arrays using recursion

Post by dbremmen@gmail.com » Thu Mar 01, 2018 7:01 pm

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);
    }
}

maty.tsoraro@gmail.com
Posts: 32
Joined: Thu Dec 21, 2017 10:06 pm

Re: merging 2 arrays using recursion

Post by maty.tsoraro@gmail.com » Thu Mar 01, 2018 11:08 pm

hi,
yes it works :)
is there a way we excute it without a for loop in the main ?

dbremmen@gmail.com
Posts: 63
Joined: Sun Jan 07, 2018 6:30 pm

Re: merging 2 arrays using recursion

Post by dbremmen@gmail.com » Fri Mar 02, 2018 11:13 pm

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

maty.tsoraro@gmail.com
Posts: 32
Joined: Thu Dec 21, 2017 10:06 pm

Re: merging 2 arrays using recursion

Post by maty.tsoraro@gmail.com » Sat Mar 03, 2018 4:36 pm

ok
thanks :)

Post Reply