Dear Experts Pls help on this Write a Java program that give

Dear Experts,

Pls help on this.

Write a Java program that, given two sorted arrays of N int values, prints all elements

that appear in both arrays, in sorted order. The running time of your program

should be proportional to N in the worst case.

tq.

Solution


public class Merge {
public static void main(String[] args) {
int arr1[]={2,3,7,9};
int arr2[]={4,6,8};
int merged_arr[]=Merge.merge(arr1, arr2);
System.out.println(\"Sorted Merged array is :\");
for(int i=0;i<merged_arr.length;i++)
{
System.out.print(merged_arr[i]+\" \");
}
}
public static int[] merge(int[] a, int[] b) {

int[] result = new int[a.length + b.length];
int i = 0,j = 0,k = 0;

while ((i<a.length) && (j<b.length))
{
if (a[i] < b[j])
   {
result[k++] = a[i++];
}
else
   {
result[k++] = b[j++];
   }
}

while (i < a.length)
result[k++] = a[i++];

while (j < b.length)
result[k++] = b[j++];

return result;
}
}

Dear Experts, Pls help on this. Write a Java program that, given two sorted arrays of N int values, prints all elements that appear in both arrays, in sorted or

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site