Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
Node is saved as draft in My Content >> Draft
  • Merge Sort in C

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 56
    Comment on it

    It is based on Divide and conquer approach. We first partition the list and then sort it and after sorting we combine the final list or array

    It is being merged and sort using the merge sort algorithm.

     

     

     

     

     

    This is a divide and conquer algorithm. This works as follows –


     

    1. Divide the input which we have to sort into two parts in the middle. Call it the left part and right part.
    
    Example: Say the input is -10 32 45 -78 91 1 0 -16 then the left part will be -10 32 45 -
    
    78 and the right part will be 91 1 0 6.
    
    2. Sort each of them separately. Note that here sort does not mean to sort it using some other method. We use the same function recursively.
    
    3. Then merge the two sorted parts.

     

    In our program we first split the array performed sorting into it and then after that we have merge it.

     

    #include<stdio.h>
    #include<conio.h>
    void merge(int [],int ,int ,int );
    void part(int [],int ,int );
    int main()
    {
     int arr[30];
     int i,size;
     printf("\n\t------- Merge sorting method -------\n\n");
     printf("Enter total no. of elements : ");
     scanf("%d",&size);
     for(i=0; i<size; i++)
     {
       printf("Enter %d element : ",i+1);
       scanf("%d",&arr[i]);
     }
     part(arr,0,size-1);
     printf("\n\t------- Merge sorted elements -------\n\n");
     for(i=0; i<size; i++)
     printf("%d ",arr[i]);
     getch();
     return 0;
    }
    
    
    void part(int arr[],int min,int max)
    {
     int mid;
     if(min<max)
     {
       mid=(min+max)/2;
       part(arr,min,mid);
       part(arr,mid+1,max);
       merge(arr,min,mid,max);
     }
    }
    
    
    void merge(int arr[],int min,int mid,int max)
    {
      int tmp[30];
      int i,j,k,m; 
      j=min;
      m=mid+1;
      for(i=min; j<=mid && m<=max ; i++)
      {
         if(arr[j]<=arr[m])
         {
             tmp[i]=arr[j];
             j++;
         }
         else
         {
             tmp[i]=arr[m];
             m++;
         }
      }
      if(j>mid)
      {
         for(k=m; k<=max; k++)
         {
             tmp[i]=arr[k];
             i++;
         }
      }
      else
      {
         for(k=j; k<=mid; k++)
         {
            tmp[i]=arr[k];
            i++;
         }
      }
      for(k=min; k<=max; k++)
         arr[k]=tmp[k];
    }

     

    .net

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: