"How will you merge these two arrays? Write the program
Array: A 1 18 22 43
Array: B 3 4 6 20 34 46 55
Output Array: C 1 3 4 6 18 20 22 34 43 46 55"

Answer Posted / naman patidar

public class MargeSort {
public static void main(String[] args) {
int a[] = { 2, 5, 7, 9, 10, 15 };
int b[] = { 1, 3, 4, 5, 12, 14 };
int c[] = new int[a.length + b.length];
int aIndex = 0, bIndex = 0, cIndex = 0;

while (aIndex < a.length && bIndex < b.length) {
if (a[aIndex] < b[bIndex]) {
c[cIndex++] = a[aIndex++];
} else {
c[cIndex++] = b[bIndex++];
}
}

while (aIndex < a.length) {
c[cIndex++] = a[aIndex++];
}
while (bIndex < b.length) {
c[cIndex++] = b[bIndex++];
}
for (int i = 0; i < c.length; i++) {
System.out.println(c[i]);
}
}
}

Is This Answer Correct ?    7 Yes 14 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What are the main features of c++?

733


Are there any special rules about inlining?

772


How compile and run c++ program in turbo c++?

827


What are the types of pointer?

761


What c++ is used for?

812






Which software is best for c++ programming?

757


What is setfill c++?

878


If we want that any wildcard characters in the command line arguments should be appropriately expanded, are we required to make any special provision? If yes, which?

1230


What is the difference between public, private, and protected access?

795


What is c++ vb?

820


What is prototype in c++ with example?

718


What are the two types of polymorphism?

768


Can we delete this pointer in c++?

923


Is c++ vector dynamic?

726


Define a constructor?

778