Thread: Understand mergesort with transition

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    10

    Understand mergesort with transition

    Code:
    #include<stdio.h>
    #define max 10
    void mergesort(int array[max],int low,int high);
    void merge(int array[max],int k,int low,int high);
    void main()
    {
        int array[max],low=0,high=max-1,i;
        printf("ENTER VALUE OF ARRAY TO BE SORTED\n");
        for(i=low;i<=high;i++)
        {
            scanf("%d",&array[i]);
        }
        printf("\nUNDERSTAND MERGE SORT TRANSITIONS\n");
        mergesort(array,low,high);
        printf("\nSORTED ARRAY IS\n");
        for(i=low;i<=high;i++)
        {
            printf("%d\t",array[i]);
        }
    }
    void mergesort(int array[max],int low,int high)
    {
        int k;
       if(low<high)
       {
           k=(low+high)/2;
           mergesort(array,low,k);
           mergesort(array,k+1,high);
           merge(array,k,low,high);
       }
    }
    void merge(int array[max],int k,int low,int high)
    {
        static int f=1;
        printf("level %d",f++);
        printf(" low=%2d  splitter=%2d  high=%2d ",low,k,high);
        int i,j=k+1,start,end;
        start=low;
        end=high;
         int subarray_size=high-low+1;
         int subarray[subarray_size];
         for(i=0;i<subarray_size;i++)
         {
            if(low<=k)
            {
                if(array[low]<=array[j])
                {
                    subarray[i]=array[low];
                   low=low+1;
                   continue;
                }
            }
            if(low>k)
            {
                --low;
                while(i<subarray_size)
                {
                    subarray[i++]=array[j++];
                }
                break;
            }
            if(j<=high)
            {
                if(array[j]<array[low])
                {
                    subarray[i]=array[j];
                    j=j+1;
                    continue;
                }
            }
            if(j>high)
            {
                --j;
                while(i<subarray_size)
                {
                    subarray[i++]=array[low++];
                }
                break;
            }
         }
         i=0;
         printf("\n");
         while(start<=end)
         {
             printf("\t%d",subarray[i]);
             array[start]=subarray[i];
             ++i;
             ++start;
         }
         printf("\n");
    }

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    We understand it.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User
    Join Date
    Jul 2011
    Posts
    10
    please post for any improvement in program

  4. #4
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    1. Don't use non-descriptive variable names like "k"
    2. main should be declared as returning an int and thusly return something.
    3. Variables should be declared on their own lines and always initialized
    4. A comment or two wouldn't go amiss
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Transition from Java to c++..
    By ankitsinghal_89 in forum Tech Board
    Replies: 13
    Last Post: 05-09-2009, 06:30 PM
  2. Transition table
    By The Urchin in forum C++ Programming
    Replies: 3
    Last Post: 10-05-2006, 09:42 AM
  3. C to C++ transition
    By trainee in forum C++ Programming
    Replies: 18
    Last Post: 01-09-2004, 10:06 AM
  4. cool transition effects?
    By ichijoji in forum Game Programming
    Replies: 1
    Last Post: 05-30-2003, 12:34 PM
  5. Transition between PP and OOP
    By The Gweech in forum C++ Programming
    Replies: 9
    Last Post: 08-06-2002, 03:04 PM