Given an integer array of which both first half and second half are sorted. Write a function to merge the two parts to create one single sorted array in place [do not use any extra space].
e.g. If input array is [1,3,6,8,-5,-2,3,8] It should be converted to: [-5,-2,1,3,3,6,8,8]
e.g. If input array is [1,3,6,8,-5,-2,3,8] It should be converted to: [-5,-2,1,3,3,6,8,8]
is there any constraint on TC? if not following is quadratic
ReplyDeletestart with 2 pointers,
p1: beginning of array
p2: at the middle(beginning of the second half)
compare the two,
while(p1 is does not reach middle)
if(*p2<*p1)
{
rotate the array from p1 to p2
incr p1 nad p2
}
else
incr p1
// if equal then you can incr either p1 or p2
nlogn mein to aise hi ho jayega yar
ReplyDeletey doin it in quadrtc