<< Prev : What is the difference between Merge Sort and Quick sort?

Give pseudocode for the mergesort algorithm?

  • Asked by : Anonymous
    December 26th, 2010 15:33
Replies:


Mergesort(a, left, right)

{

   if(left<right)

   {

     I=(left+right)/2;

     Mergesort(a,left, I);

     Mergesort(a,I+1,right);

     Merge(a,b,left,I,right);

     Copy(b,a,left,right);

   }

}

The merge would be something liks this

merge(int a[], int b[], int c[], int m, int n)

{

  int i, j, k;

  i = 0;

  j = 0;

  k = 0;

  while(i<=m && j<=n)

  {

    if(a[i] 
    {

        c[k]=a[i];

        i=i+1;

    }

    else

    {

        c[k]=a[j];

        j=j+1;

    }

    k=k+1;

  }

  while(i<=m)

    c[k++]=a[i++];

  while(j<=n)

    c[k++]=a[j++];

}

To reply to this discussion, please