Code Scrappers


Merge Sort C Program


                    #include<stdio.h>
                    #include<stdlib.h>

                    void mergesort(int[], int, int);

                    void merge(int[], int, int);

                    void main()

                    {
                            int n, a[10], i, j;

                            printf("enter the size of the array\n");

                            scanf("%d",&n);

                            printf("enter the array elements\n");

                            for(i=0;i<n;i++)
                            {
                                    scanf("%d",&a[i]);
                            }

                            mergesort(a,0,n-1);

                            printf("array after sorting is \n");

                            for(i=0;i<n;i++)
                            {
                                    printf("%d\n",a[i]);
                            }
                    }

                    void mergesort(int a[], int low, int high)

                    {
                            int mid;

                            if(low>=high)
                            {
                                    return;
                            }
                            else
                            {
                                    mid = (low + high)/2;

                                    mergesort(a, low, mid);

                                    mergesort(a, mid+1, high);

                                    merge(a, low, high);

                            }
                     }

                     void merge(int a[], int low, int high)

                    {
                          int mid = (low + high)/2;

                          int k,i, j, temp[10];

                          i = low;

                          j = mid+1;

                          k = low;

                          while(i<=mid && j<= high)
                          {
                                if(a[i] < a[j])
                                {
                                      temp[k++] = a[i++];
                                }

                                else
                                {
                                      temp[k++] = a[j++];
                                }
                          }

                          while(i<=mid)
                          {
                                temp[k++] = a[i++];
                          }

                          while(j<=high)
                          {
                                temp[k++] = a[j++];
                          }


                          for(i=low;i<=high;i++)
                          {
                                a[i] = temp[i];
                          }
                    }


                



Output

            enter the size of the array
            5
            enter the array elements
            1
            4
            2
            8
            7
            array after sorting is
            1
            2
            4
            7
            8