Friday, May 24, 2013

MERGE SORT Algorithm implementation / Application of Divide-and-conquer Method

 /***********************************************************
* You can use all the programs on  www.engineercse.blogspot.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact azam.ruet10.com
* To find more C programs, do visit www.engineercse.blogspot.com
* and browse!
*
*                        Coding is poetry!!!
***********************************************************/
#include
#include
#define MAX_ARY 10
int arr[MAX_ARY];
void merge_sort(int low, int high);
void Merge(int low, int mid, int high);

int main(void)
{

    int j =0;
    printf("\n\nEnter the elements to be sorted:\n");
    for(j=0;j      scanf("%d",&arr[j]);
      printf("\n");
      printf("Before Merging:");
      for(j=0;j      printf("%d ",arr[j]);
    merge_sort(0,MAX_ARY-1);
    printf("After Merging::");
    for(j=0;j    printf("%d  ", arr[j]);

    getch();
}

void merge_sort(int low, int high)
{

    int mid;

    if(low    {
    mid = (low+high)/2;
    merge_sort(low, mid);
    merge_sort(mid+1 , high);
    Merge(low,mid,high);
    }
}

void Merge(int low, int mid, int high){
    int b[MAX_ARY],k;
   int h = low;
   int i = low;
   int j = mid+1;
   while((h<=mid) && (j<=high))
   {
      if(arr[h]<=arr[j]){
         b[i] = arr[h];
         h++;
      }
      else
      {
          b[i] = arr[j];
          j++;
      }
      i++;
   }
      if(h>mid)
      {
         for( k=j;k<=high;k++)
         {
            b[i]=arr[k];
            i++;
         }
      }
      else
      for(k=h; k<=mid;k++)
      {
          b[i] = arr[k];
          i++;
      }
   for(k=low;k<=high;k++)
   arr[k] = b[k];
}


get CSE 402 LAB codes CSE402 LAB practise

No comments:

Post a Comment