362 views
0 votes
0 votes
#include<stdio.h>
void mergeArray(int arr[],int l,int m,int r)
{
int n1=m-l+1;
int n2=r-m;
int Larr[n1];
int Rarr[n2];
int i,j,k;
for(i=0;i<n1;i++)
    Larr[i]=arr[l+i];
for(j=0;j<n2;j++)
    Rarr[j]=arr[m+1+j];

i=0;
j=0;
k=l;
while(i<n1&&j<n2)
{
    if(Larr[i]<Rarr[j])
    {
        arr[k]=Larr[i];
        i++;
    }
    else
    {
        arr[k]=Rarr[j];
        j++;
    }
    k++;
}
if(i<n1)
{
arr[k]=Larr[i];
i++;
k++;
}

if(j<n2)
{
arr[k]=Rarr[j];
j++;
k++;
}

}
void merge(int arr[],int l,int r)
{

if(l<r)
{
int m=l+(r-l)/2;

merge(arr,l,m);
merge(arr,m+1,r);
mergeArray(arr,l,m,r);
}

}

int main()
{
int n;
scanf("%d",&n);
int arr[n],i;
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}

merge(arr,0,n-1);
printf("[");
for(i=0;i<n-1;i++)
    printf("%d,",arr[i]);

    printf("%d]",arr[n-1]);
return 0;
}
 

In this code when i am giving the input n=5;

2 3 1 4 5

than it will give 1 2 3 1 2

where is the problem can anyone explain ?

Please log in or register to answer this question.

Related questions

1 votes
1 votes
2 answers
2
Shubhanshu asked Dec 1, 2018
5,097 views
Is Quick sort an adaptive sorting Algorithm? I think no. Because as per the definition given in the Wikipedia is that A adaptive sorting Algorithm is one who takes the ad...
1 votes
1 votes
0 answers
3
1 votes
1 votes
1 answer
4
rahul sharma 5 asked Mar 9, 2018
1,320 views
Consider the modified merge sort where we divide array into 5 equal sub arrays instead if 2(as in standard merge sort).What is the time complexity if modified merge sort?...