#include<stdio.h>

int arr[20];

int main()
{
  int n,i;

  printf("Enter the size of array\n");
  scanf("%d",&n);
  printf("Enter the elements:");
  for(i=0; i<n; i++)
    scanf("%d",&arr[i]);

  merge_sort(arr,0,n-1);

  printf("Sorted array:");
  for(i=0; i<n; i++)
    printf(" \n %d",arr[i]);

  return 0;
}

int merge_sort(int arr[],int low,int high)
{
  int mid;
  if(low<high) {
    mid=(low+high)/2;

    merge_sort(arr,low,mid);
    merge_sort(arr,mid+1,high);
    // Combine
    merge(arr,low,mid,high);
  }

  return 0;
}

int merge(int arr[],int l,int m,int h)
{
  int arr1[10],arr2[10];

  int n1,n2,i,j,k;
  n1=m-l+1;
  n2=h-m;

  for(i=0; i<n1; i++)
  {
    arr1[i]=arr[l+i];
  }
  for(j=0; j<n2; j++)
  {
    arr2[j]=arr[m+j+1];
  }
  arr1[i]=9999;
  arr2[j]=9999;

  i=0;
  j=0;
  for(k=l; k<=h; k++) {
    if(arr1[i]<=arr2[j])
      arr[k]=arr1[i++];
    else
      arr[k]=arr2[j++];
  }

  return 0;
}

in this my doubt is here

int merge_sort(int arr[],int low,int high)
{
  int mid;
  if(low<high) {
    mid=(low+high)/2;

    merge_sort(arr,low,mid);
    merge_sort(arr,mid+1,high);
    // Combine
    merge(arr,low,mid,high);
  }

  return 0;
}

what will be the value of high???//

for example take this output as scenario
size of array is :5
elements :10,50,2,3,1

now my value of n is 5 and so after the merge_sort function the values will low=0 and high=4 after these two function

 merge_sort(arr,low,mid);
    merge_sort(arr,mid+1,high);

what will be the value of mid,low,high????i am confused i dont understand pleas help at this point

Recommended Answers

All 4 Replies

line 10 is impossible because merge_sort() does not take that many parameters. The number of parameters on line 10 have to match exactly the parameters on line 1. You can give them different variable names, but the number and type of parameters must be the same.

The value of mid is calculated on line 5.

ok is this code wrong??????

hello am re-opening this thread after 1 month.i found a code of merge sort like this can we write like this ????

#include <stdio.h>
#include <conio.h>

void main( )
{
    int a[5] = { 11, 2, 9, 13, 57 } ;
    int b[5] = { 25, 17, 1, 90, 3 } ;
    int c[10] ;
    int i, j, k, temp ;

    clrscr( ) ;

    printf ( "Merge sort.\n" ) ;

    printf ( "\nFirst array:\n" ) ;
    for ( i = 0 ; i <= 4 ; i++ )
        printf ( "%d\t", a[i] ) ;

    printf ( "\n\nSecond array:\n" ) ;
    for ( i = 0 ; i <= 4 ; i++ )
        printf ( "%d\t", b[i] ) ;

    for ( i = 0 ; i <= 3 ; i++ )
    {
        for ( j = i + 1 ; j <= 4 ; j++ )
        {
            if ( a[i] > a[j] )
            {
                temp = a[i] ;
                a[i] = a[j] ;
                a[j] = temp ;
            }
            if ( b[i] > b[j] )
            {
                temp = b[i] ;
                b[i] = b[j] ;
                b[j] = temp ;
            }
        }
    }

    for ( i = j = k = 0 ; i <= 9 ; )
    {
        if ( a[j] <=  b[k] )
            c[i++] = a[j++] ;
        else
            c[i++] = b[k++] ;

        if ( j == 5 || k == 5 )
            break ;
    }

    for ( ; j <= 4 ; )
        c[i++] = a[j++] ;

    for ( ; k <= 4 ; )
        c[i++] = b[k++] ;

    printf ( "\n\nArray after sorting:\n") ;
    for ( i = 0 ; i <= 9 ; i++ )
        printf ( "%d\t", c[i] ) ;

    getch( ) ;
}

Does it work?
If so, probably. Test it with a lot more data.
If not, no.

Please don't use getch() at the end of programs. It's non-standard and when you get to a real compiler, you'll have problems.

What to replace it with? Well, what does getch() do? What standard statements do the same thing? Use that instead.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.