Thread: Merge sort problem

  1. #1
    Registered User thriller500's Avatar
    Join Date
    Oct 2011
    Posts
    25

    Merge sort problem

    i was writing a recursive code for mergesort .. and when i am executing it.
    after entering the array .. program stopped working..
    Please help me...
    here's the code..

    Code:
    #include<stdio.h>
    #include<conio.h>
    #define MAX 20
    int a[MAX];
    int n;
    int main()
    {
        int i,j,k,l,m;
        printf("ENTER THE NUMBER OF TERMS\n");
        scanf("%d",&n);
        printf("ENTER THE ARRAY\n");
        for(i=0;i<n;i++)
        {
                         scanf("%d",&a[i]);
        }
                   
          printf("Before Sorting\n");
          
        for(i=0;i<n;i++)
        {
                         printf("%d\n",a[i]);
        }                    
          printf("\n");
          
          mergesort(0,n-1);
          printf("AFTER SORTING");
              for(i=0;i<n;i++)
        {
                         printf("%d\n",a[i]);
        }                  
        
        
        
        
        getch();
                }
                
                
                
                mergesort(int low,int high)
                {
                          int mid;
                 
                    
                          if (low<high)
                          mid=(low+high)/2;
                          
                         mergesort(low,mid);
                         mergesort(mid+1,high);
                         merge(a,low,mid,high);
                         }
       merge( int a[],int low,int mid,int high )       
            {
                 int i=0,j,k,lb;
                 int b[high];
                 lb=i=low,
                 j=mid+1;
                 while((lb<=mid)&&(j<high))
                 {
                                           if(a[lb]<=a[j])
                                           {
                                                          b[i]=a[lb];
                                           lb=lb+1;
                                           }
                                           else
                                           {
                                               b[i]=a[j];
                                               j=j+1;
                                               }
                                               
                                           i=i+1;
                                           }
            if(lb>mid)
            {
                      for(k=j;k<=high;k++)
                      {
                               b[i]=a[k];
                               i=i+1;
                               }}
                               
                               else                                              
                               {
                                  for(k=lb;k<=mid;k++)
                                  {
                                          b[i]=a[k];
                                          i=i+1;
                                          }
                                          }
                                          
                                          
        for(k=low;k<=high;k++)
        {
            a[k]=b[k];
            }
            }

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by thriller500 View Post
    i was writing a recursive code for mergesort .. and when i am executing it.
    after entering the array .. program stopped working..
    Please help me...
    You are going to have to figure out how to perform a better diagnosis than that if you want to write a mergesort. What does "stopped working" mean? Exactly where and when did it stop working? This might require throwing in some fprintf(stderr, ...) statements, it might mean using a debugger, it might mean both.

    If that is too much for you, then give up on mergesort for a while and try some simpler exercises (bubble sort, insertion sort, etc).

    The other thing you have to do is correctly indent your code. That post is an ABSURD MESS.

    Indent style - Wikipedia, the free encyclopedia

    Pick one of the styles there and stick too it. When you post to the forum, make sure it looks like it should. If it doesn't, figure out why. Everybody else does this, there is are no excuses for you.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User thriller500's Avatar
    Join Date
    Oct 2011
    Posts
    25
    sorry i am new to coding.. had no idea about these styles..
    it stopped working in the sense..
    i am facing a problem somewhere in the functions...
    i named the file as merge sort..
    after i entered the array..
    when the execution reaches the mergesort function call

    program stopped working..
    windows is checking for a solution to the problem..

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    "program stopped working..windows is checking for a solution to the problem.".

    that typically means you accessed an illegal memory location or you corrupted the stack. in your case you are probably overrunning an array on the stack. and you have an array on the stack in your function 'merge'.

    in your loops, check out everywhere you use '<=' which can be a source of overruns if your index goes 1 too many. like 'k<=high'.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Please give an example of the data you are entering that causes the crash.
    Then also try simpler examples to try and narrow down the problem. Always start with the simplest possible cases and make sure they work first. I.e.
    What happens for zero items?
    What happens for just one item?
    What happens for two items that are already in order?
    What happens for two items that are not initially in order?
    What happens for 10 items that are already in order?

    Once you've tried those, show us what you discovered.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Urgent problem with a merge sort.
    By jonnoblood in forum C Programming
    Replies: 7
    Last Post: 02-10-2010, 08:35 PM
  2. Merge sort fails to sort
    By Cresh07 in forum C++ Programming
    Replies: 3
    Last Post: 09-23-2009, 11:17 AM
  3. Quick Sort or Merge Sort???
    By swanley007 in forum C++ Programming
    Replies: 6
    Last Post: 11-10-2005, 06:48 PM
  4. Quick sort VS Merge Sort
    By sachitha in forum Tech Board
    Replies: 7
    Last Post: 09-03-2004, 11:57 PM
  5. merge sort and selection sort and time spent on both
    By misswaleleia in forum C Programming
    Replies: 3
    Last Post: 06-04-2003, 02:24 PM