"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
Can class objects be passed as function arguments?
Can a function take variable length arguments, if yes, how?
What is flush c++?
Write some differences between an external iterator and an internal iterator?
What is c++ and its features?
Can you please explain the difference between static and dynamic binding of functions?
What is a c++ vector?
Which is the best c++ software?
What is the extraction operator and what does it do?
What is string in c++ programming?
How many types of classes are there in c++?
Explain data encapsulation?
What is virtual methods?
What is a virtual destructor? Explain the use of it?
How is objective c different from c++?