import java.util.*;
public class MyClass {
public static void main(String[] args) {
int arr1[]={1,3,5,7,9};
int arr2[]={2,4,6,8,10};
int ans[]=new int[10];
ans = merge(arr1,arr2);
for(int i=0;i<10;i++)
{
System.out.print(ans[i]+" ");
}
}
public static int[] merge(int arr1[], int arr2[])
{
int i=0,j=0,k=0;
int n1=arr1.length;int n2=arr2.length;int ans[]=new int[n1+n2];
while (i < n1 && j < n2) {
if (arr1[i] < arr2[j]) {
ans[k++] = arr1[i++];
} else {
ans[k++] = arr2[j++];
}
}
// Copy the remaining elements of arr1, if any
while (i < n1) {
ans[k++] = arr1[i++];
}
// Copy the remaining elements of arr2, if any
while (j < n2) {
ans[k++] = arr2[j++];
}
return ans;
}
}
No comments:
Post a Comment