"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


Please Help Members By Posting Answers For Below Questions

What is the benefit of learning c++?

549


Why do we use pointers in c++?

589


Are vectors faster than arrays?

569


What is virtual table?

621


Is it possible to get the source code back from binary file?

715






What is a rooted hierarchy?

683


Explain what data encapsulation is in c++?

572


What is the identity function in c++? How is it useful?

553


How the endl and setw manipulator works?

558


What is c++ code?

587


What are arithmetic operators?

539


daily Routine of father

907


What is c++ vb?

614


write a c++ program to create class student having datamember name,Roll_no,age,and branch intilcization all the member using constructor print the all the details on the screen.

2249


How to allocate memory dynamically for a reference?

544