Menu BAR

FEEL FREE TO ASK ANY PROGRAM and please mention any error if you find it

24 Jan 2013

Algo for MErge sort using PSUEDO-CODE

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