some one help me pls..
segmentation fault for merge_sort....:(

#include<stdlib.h>
#include<stdio.h>
void merge_sort(int [],int ,int );
void merge(int [],int ,int , int);
int main()
{
        int i,n,a[25];
        printf("enter the no of elements:");
        scanf("%d",&n);
        for(i=0;i<n;i++)
        {
                printf("enter the element:");
                scanf("%d",&a[i]);
        }
        merge_sort(a,0,n-1);

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

}



void merge_sort(int a[],int p,int q)
{
        if(p<q)
        {
                merge_sort(a,p,p+q/2);
                merge_sort(a,(p+q/2)+1,q);
                merge(a,p,p+q/2,q);
        }
}



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

        int i,j[30],l,m,k,g;
        l=low;
        m=mid+1;
        i=low;
        while(l<=mid && m<=high)
        {
                if(a[l]<=a[m])
                {
                        j[i]=a[l];
                        l++;
                }
                else
                {
                        j[i]=a[m];
                        m++;
                }
                i++;
        }
        if(m>high)
        {
                for(g=l;g<=mid;g++)
                {
                        j[i]=a[g];
                        i++;
                }
        }
        else

        {
                for(g=m;g<=high;g++)
                {
                        j[i]=a[g];
                        i++;
                }
        }
        for(i=low;i<=high;i++)
        {
                a[i]=j[i];
        }

}

And you called this a seg fault on a bubble sort, why??

Anyway. I don't use merge sort much, but p+q/2 will be processed as p + (q/2), and I don't believe that's what you want. I use an m (middle) variable, myself.

l(left) and r(right), correspond to your p and q variables:

void mergesort(int *A, int *Index, int l, int r) {
   int m;
   if (l < r) {
      m = (l + r) /2;
      mergesort(A, Index, l, m);
       mergesort(A, Index, m + 1, r);
      merge(A, Index, l, m, r);
   }
}

Although we're using the same algorithm, your merge() function using different variable names. That last copy back in merge(), looks suspect, since my version only copies the remaining first half (if any):

/* copy back remaining elements of first half (if any)  */
    while (i <= m)
        A[k++] = Index[i++];

Your version only increments i, while my version increments two variables, i and k. I don't believe the match up between the two indices will allow just one variable to work.

But then again, I don't use merge sort much at all. Those are the area's I'd look at first, as suspect.

Segmentation fault is due to an attempt of access of an area of memory that is not allowed to be accessed.
Common causes are:-
Improper format control string in printf or scanf statements.
Forgetting to use & on the arguments to scanf.
Accessing beyond the boundaries of an array.
Failure to initialize a pointer before accessing it.
Incorrect use of the "&" (address of) and "*" (dereferencing) operators.

Your fault:-

merge_sort(a,p,p+q/2);

merge(a,p,p+q/2,q);

you did not give proper brackets.
it should be:--

merge_sort(a,p,(p+q)/2);
                merge_sort(a,(p+q/2)+1,q);
                merge(a,p,(p+q)/2,q);

now your code will work fine.:)

This article has been dead for over six months. Start a new discussion instead.