GeeksforGeeks » C/C++ Programming Questions
an optimum algorithm
(3 posts)-
pls suggest an 0(n) algorithm..
-
(This is the problem):
You are given two sorted arrays, A and B, and A has a large enough
buffer at the end to
hold B. Write a method to merge B into A in sorted order. -
Use the concept of merge sort...
start from the last element of A and B, find the max between those two elements and copy it to the A's last position.
do this for all elements.eg:
A={5,7,13,25,36,50} len=6B={3,6,10,35,55} len=5
then do merging
A[10]=max(50,55)=55, i=5, j=4
A[9]=max(50,35)=50, i=5, j=3
A[8]=max(36,35)=36, i=4, j=3 and so onfinally A={3,5,6,7,10,13,25,35,36,50,55} len=11
Time complexity O(m+n)
Reply
You must log in to post.