"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


Please Help Members By Posting Answers For Below Questions

On throwing an exception by the animal constructor in p = new animalq, can memory leak occur?

865


You want to link a c++ program to c functions. How would you do it?

712


How would perform Pattern Matching in C++?

909


What are the various operations performed on stack?

799


What is c++ vb?

820






What are the differences between malloc() and calloc()?

829


Write an algorithm that determines whether or not an almost complete binary tree is a heap.

3649


Explain how to initialize a const member data.

776


What are the new features that iso/ansi c++ has added to original c++ specifications?

785


What does it mean to declare a member variable as static?

793


Can we define function inside main in c++?

750


What is the difference between equal to (==) and assignment operator (=)?

762


What methods can be overridden in java?

895


Is c++ vector dynamic?

726


What is c++ mutable?

876