"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
What is c++ virtual inheritance?
What is difference between n and endl in c++?
What is the use of namespace std in C++?
what are the iterator and generic algorithms.
explain the reference variable in c++?
Why Pointers are not used in C++?
What is the need of a destructor? Explain with the help of an example.
Why do we use double in c++?
Are strings immutable in c++?
Draw a flow chart and write a program for the difference between the sum of elements with odd and even numbers. Two dimensional array.
Can you please explain the difference between overloading and overriding?
How the memory management in vectors are being done. What happens when the heap memory is full, and how do you handle it ?
Can we change the basic meaning of an operator in c++?
What is #include cmath?
We use library functions in the program, in what form they are provided to the program?