can anyone please tell me what is wrong with dis code.....i am getting a segmentation fault even though it has been compiled successfully(in a unix system):

#include<stdio.h>

int low,high,mid;

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

int main()
{
 int a[10];
 int count;
 for(count=0;count<10;count++)
  {
    printf("Enter element number %d\n",count+1);
    scanf("%d" , &a[count]);
  }
 low=a[0];
 high=a[9];
 msort(a,low,high);
 
 for(count=0;count<10;count++)
  {
   printf("%d\n" ,a[count]);
  }
 
}



void msort(int a[],int low,int high)
{
 int mid;
 if(low<high)
  {
   mid=(low+high)/2;
   msort(a,low,mid);
   msort(a,mid+1,high);
   merge(a,low,high,mid);
  }
 
}

  
 
void merge(int a[],int low,int high,int mid)
{
 int b[50];
 int i,j,k;
 i=low;
 j=mid+1;
 k=low;
 
 while((i<=mid)&&(j<=high))
  {
   if(a[i]<a[j])
    {
      b[k]=a[i];
      i++;
      k++;
    }
   else
    {
      b[k]=a[j];
      j++;
      k++;
    }
   }
 
while(i<=mid)
 {
  b[k]=a[i];
  i++;
 }

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

for(i=0;i<high;i++)
 {
  a[i]=b[i];
 }
}

Edited 6 Years Ago by WaltP: Added CODE Tags

First, Welcome to the Forum, Kanokan! ;)

Second - it's a real pain to study code that isn't looking like code, because you didn't surround it with code tags (the code icon in the editor window gives you what you need to put your code between).

Just because code will compile, doesn't mean it will run and not crash. Being able to compile the code, is just the start.

I'll look at your code -- back after awhile.

#include<stdio.h>
#define MAX 9

void merge(void);

int a[]={112,68,-34,66,76,89,4,36,2};

void main()
{
  int i=0;
  clrscr();
  printf("\noriginal numbers \n");
  for(i=0;i<MAX;i++)
      printf("\t%d",a[i]);
  merge();
  printf("\nSorted numbers \n");
  for(i=0;i<MAX;i++)
      printf("\t%d",a[i]);
  return;

void merge()
{
  int s=1,i=0,j=0,k=0,x=0,y=0,u1,u2;
  int b[MAX];
  /*
   b  : new temperory array
   x  : lower bound of the first array
   u1 : upperbound of the first array
   u2 : upperbound of the second array
   k  : subscript for array b
   i  : subscript for array a's first subarray
   j  : subscript for array a's second subarray
   MAX: maximum number of elements
   s  : size of the subarray
 */
  while(s<MAX)
  {
    k=0;
    x=0;
    while( (x+s) <= MAX)
    {
     y=x+s;
     u1=y-1;
     if(y+s<MAX)
         u2=u1+s;
     else
         u2=MAX-1;
    j=y;
    i=x;
    /* comparing elements fronm both subarrays */
    while(i <= u1 && j <= u2 )
    {
        if(a[i] < a[j])
            b[k++]=a[i++];
        else
            b[k++]=a[j++];
    }
    /*copying the rest of the contents form 2nd subarray to new array */
    while(j <= u2)
        b[k++]=a[j++];
    /*copying the rest of the contents form 1st subarray to new array */
    while(i <= u1)
        b[k++]=a[i++];
    x=u2+1;
    }

    s=s*2;
    /*copying contents form the new array to the original array */
    for(i=0;i<MAX;i++)
      a[i]=b[i];

    printf("\n displaying the contents of the array \n");
    for(i=0;i<MAX;i++)
       printf("\t%d",a[i]);
  }
 return;
 }

Edited 3 Years Ago by Reverend Jim: Fixed formatting

#include<stdio.h>
#define MAX 9

void merge(void);

int a[]={112,68,-34,66,76,89,4,36,2};

void main()
{
  int i=0;
  clrscr();
  printf("\noriginal numbers \n");
  for(i=0;i<MAX;i++)
      printf("\t%d",a[i]);
  merge();
  printf("\nSorted numbers \n");
  for(i=0;i<MAX;i++)
      printf("\t%d",a[i]);
  return;
}

void merge()
{
  int s=1,i=0,j=0,k=0,x=0,y=0,u1,u2;
  int b[MAX];
  /*
   b  : new temperory array
   x  : lower bound of the first array
   u1 : upperbound of the first array
   u2 : upperbound of the second array
   k  : subscript for array b
   i  : subscript for array a's first subarray
   j  : subscript for array a's second subarray
   MAX: maximum number of elements
   s  : size of the subarray
 */
  while(s<MAX)
  {
    k=0;
    x=0;
    while( (x+s) <= MAX)
    {
	 y=x+s;
	 u1=y-1;
	 if(y+s<MAX)
	     u2=u1+s;
	 else
	     u2=MAX-1;
	j=y;
	i=x;
	/* comparing elements fronm both subarrays */
	while(i <= u1 && j <= u2 )
	{
		if(a[i] < a[j])
			b[k++]=a[i++];
		else
			b[k++]=a[j++];
	}
	/*copying the rest of the contents form 2nd subarray to new array */
	while(j <= u2)
		b[k++]=a[j++];
	/*copying the rest of the contents form 1st subarray to new array */
	while(i <= u1)
		b[k++]=a[i++];
	x=u2+1;
    }

    s=s*2;
    /*copying contents form the new array to the original array */
    for(i=0;i<MAX;i++)
      a[i]=b[i];

    printf("\n displaying the contents of the array \n");
    for(i=0;i<MAX;i++)
       printf("\t%d",a[i]);
  }
 return;
 }

Edit:
=====
The first problem I found with your code was the assignment of low and high to a[0] and a[9]. Low and high, should be index numbers, not values. On your first call, since nothing has been partitioned yet, the low and high should be 0 and array size -1.

I changed a bunch of other variables, simply to save time.

#include<stdio.h>

//int low,high,mid; These should be local, not global variables

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

int main()
{
    int a[10];
    int count;
    for(count=0; count<10; count++)
    {
        printf("Enter element number %d\n",count+1);
        scanf("%d" , &a[count]);
    }
    partition(a,0,9);

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

/*high is array[SIZE]-1 */
void merge(int a[],int low,int mid,int high)
{
  int i,j,k,l,b[20];
  l=low;
  i=low;
  j=mid+1;
  while((l<=mid)&&(j<=high))
  {
    if(a[l]<=a[j])
    {
      b[i]=a[l];
      l++;
    }
    else
    {
      b[i]=a[j];
      j++;
    }
    i++;
  }
  if(l>mid)
  {
    for(k=j;k<=high;k++)
    {
      b[i]=a[k];
      i++;
    }
  }
  else
  {
    for(k=l;k<=mid;k++)
    {
      b[i]=a[k];
      i++;
    }
  }
  for(k=low;k<=high;k++)
  {
    a[k]=b[k];
  }
}

void partition(int a[],int low,int high)
{
  int mid;
  if(low<high)
  {
    mid=(low+high)/2;
    partition(a,low,mid);
    partition(a,mid+1,high);
    merge(a,low,mid,high);
  }
}

This's my implementary of merge sorting.

#include<stdio.h>
#define N (11)        // size of sorting array
void msort(int a[],int low,int high);
void merge(int a[],int low,int high,int mid);

int main()
{
 int low=0,high=N-1,count;
 int a[N]={0,9,7,8,6,0,2,1,5,4,3};   // original data

 printf("Before sorting: ");
 for(count=0;count<N;count++)
   printf("%d " ,a[count]);
 putchar('\n');

 msort(a,low,high);
 
 printf("After  sorting: ");
 for(count=0;count<N;count++)
   printf("%d " ,a[count]);
 putchar('\n');

 return 0;
}

void msort(int a[],int low,int high)  // driver program fo merge sorting
{
 int mid;
 if(low<high)
  {
   mid=(low+high)/2;
   msort(a,low,mid);
   msort(a,mid+1,high);
   merge(a,low,high,mid);
  }
}
 
void merge(int a[],int low,int high,int mid)
{
 int b[N];  // temp array
 int i=low, j=mid+1, k=low;
 
 while((i<=mid)&&(j<=high))
   if(a[i]<a[j])
      b[k++]=a[i++];
   else
      b[k++]=a[j++];
 
 while(i<=mid)
   b[k++]=a[i++];

 while(j<=high)
   b[k++]=a[j++];

 for(i=low;i<=high;i++)  // copy the sorted data to original array
   a[i]=b[i];
}

thank u .....evryone.....thank u for ur help..!!
dis site is grt...!!

> n of crse u ppl are grt..!!

Just letting you know that I think your keyboard might need to be replaced.

This question has already been answered. Start a new discussion instead.