Q.) Write a ALGORITHM for the set of elements using the MERGE SORT.
Sol)
MergeSort( A, p, r)
{
if (p<r)
{
mid=[(p+r)/2]
}
MergeSort(A, p, mid)
MergeSort(A, mid+1, r)
Merge(A, p, r, mid, Z)
}
Merge(A, p, r, mid, Z)
{
Initialize two variables 'i' and 'j'
while( (i<=mid) AND (j<=r) )
{
if(x[i]<=x[j])
{
Z={x[i]}
i=i+1
}
else
{
Z={x[j]}
j=j+1
}
}
if( i>mid )
{
Z= Z + Append(x[j]....x[r])
}
else
{
Z=Z + Append(x[i].....x[mid])
}
}
No comments:
Post a Comment