@dumanshu check it
Algo is simply start putting elemnt in bigger array by comparing then
from last logic is same as merge part of merg sort :)
void merge(int[] a, int[] b, int n, int m)
{
int k = m + n - 1; // Index of last location of array b
int i = n - 1; // Index of last element in array b
int j = m - 1; // Index of last element in array a
// Start comparing from the last element and merge a and b
while (i >= 0 && j >= 0)
{
if (a[i] > b[j])
{
a[k--] = a[i--];
}
else
{
a[k--] = b[j--];
}
}
while (j >= 0)
{
a[k--] = b[j--];
}
//no need to do for a array as its alraedy filled in B array :)
}
Time O(N)
Thanks
Shashank Mani Narayan
Computer Science & Engg.
Birla Institute of Technlogy Mesra
--
You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/algogeeks?hl=en.