"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
above problem can be solved using marge sort technique.
here i am giving a solution in java.
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++];
}
}
if (aIndex < a.length) {
while (aIndex < a.length) {
c[cIndex++] = a[aIndex++];
}
}
if (bIndex < b.length) {
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 ? | 12 Yes | 7 No |
Post New Answer View All Answers
What is #include iomanip?
Write down the equivalent pointer expression for referring the same element a[i][j][k][l]?
what do you mean by volatile variable?
daily Routine of father
What is atoi in c++?
What is a c++ object?
Explain terminate() and unexpected() function?
Define Virtual function in C++.
What is ofstream c++?
Evaluate the following expression as C++ would do :8 * 9 + 2 * 5 a) 82 b) 79 c) 370 d) list
What is c++ array?
What is the difference between a "copy constructor" and an "assignment operator" in C++?
What are the various compound assignment operators in c++?
Can we distribute function templates and class templates in object libraries?
What is the difference between an external iterator and an internal iterator? Describe an advantage of an external iterator?