C programming ..............1


#1

I am not getting the desired output in the code given below.
PLEASE RECTIFY THE ERROR.
#include <stdio.h>
#include<stdlib.h>
void merge_sort(int *a,int l,int r);
void printArray(int *a,int n);
void merge(int *a,int l,int m,int r);
int main(void) {
int n,l=0;
scanf("%d",&n);
int *arr=(int *)malloc(sizeof(int)*n);
for(int i=0;i<n;i++){
scanf("%d",&arr[i]);
}
merge_sort(arr,l,n);
printArray(arr,n);

return 0;

}
void merge_sort(int*a,int l,int r){
if(l<r){
int m=l+(r-l)/2;
merge_sort(a,l,m);
merge_sort(a,m+1,r);
merge(a,l,m,r);
}
}
void merge(int *arr,int l,int m,int r){
int n1=m-l+1;
int n2=r-m;
int L[n1],R[n2];
for(int i=0;i<n1;i++){
L[i]=arr[i+l];
}
for(int j=0;j<n2;j++){
R[j]=arr[m+1+j];

}
int i=0;int j=0;int k=0;
while(i<n1 && r<n2){
	if(L[i]<R[j]){
		arr[k]=L[i];
		i++;
	}
	else{
		arr[k]=R[j];
		j++;
	}k++;}
while(i<n1){
	arr[k]=L[i];
	i++;k++;
	}
	while(j<n2){
		arr[k]=R[j];

}