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