"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
When are exception objects created?
Define a constructor?
What are the various operations performed on stack?
Do class declarations end with a semicolon?
What is the use of endl?
Do we have to use initialization list in spite of the assignment in constructors?
How many types of modularization are there in c++?
Define the process of handling in case of destructor failure?
What is lambda expression c++?
Write a function to perform the substraction of two numbers. Eg: char N1="123", N2="478", N3=-355(N1-N2).
What are the differences between new and malloc?
Is c++ a good first language to learn?
In a function declaration, what does extern mean?
What are the methods of exporting a function from a dll?
Write some differences between an external iterator and an internal iterator?