C Program to sort elements using MERGE SORT

C programming code for  Merge SORT  to sort numbers or arrange them in ascending order.

#include <stdio.h>
    #include <conio.h>
    int merge(int *left,int *right,int *result,int nl,int nr)
    {
        int i=0,j=0,k=0;
        while(nl>0 && nr>0)
        {
            if(left[i] <= right[j])
            {
                result[k] = left[i];
                i++;
                k++;
                nl–;
            }
            else
            {
                result[k] = right[j];
                j++;
                k++;
                nr–;
            }
        }
        while(nl>0)
        {
            result [k] = left[i];
            i++;
            k++;
            nl–;
        }
        while(nr>0)
        {
            result[k] = right[j];
            j++;
            k++;
            nr–;
        }
    }

    int main()
    {
        int a[10],b[10],x,y;
        int c[20],i;
        printf(“\nThe no of element in first array : “);
        scanf(“%d”,&x);
        printf(“enter %d no of digits\n\n”,x);
        for (int i=0;i<x;i++)
        {
        printf(“%d no : “,i+1);
        scanf(“%d”,&a[i]);
        }
        printf(“\nThe no of element in 2nd array : “);
        scanf(“%d”,&y);
        printf(“enter %d no of digits\n\n”,y);
        for (int i=0;i<y;i++)
        {
        printf(“%d no : “,i+1);
        scanf(“%d”,&b[i]);
        }
        merge(a,b,c,x,y);
        printf(“\n The sorted list is: “);

        for(i=0;i<x+y;i++)
            printf(“\n %d”,c[i]);
  getch();
 }

OUTPUT

You might be also interested in :

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s