Merge Sort - Hi I tried to write the solution but my solution is not working and I am not able to figure out where things are wrong

My Program :

#include
using namespace std;
#define li long int
li temp[20005];
void merge(li *a,li s,li e,li mid)
{
li i=s,j=mid+1,k=s;
while(i<=mid&&j<=e)
{
if(a[i]<a[j])
temp[k++] = a[i++];
else
temp[k++] = a[j++];
}
while(i<=mid)
temp[k++] = a[i++];
while(j<=e)
temp[k++] = a[j++];
//for(int i=s;i<k;i++)
// a[i] = temp[i];
}
void mergeSort(li *a,li s,li e)
{
if(s>=e)
return;
li mid = s + (e-s)/2;
//cout<<“Inside merge sort”;
mergeSort(a,s,mid);
mergeSort(a,mid+1,e);

merge(a,s,e,mid);

}
int main() {
li n;
cin>>n;
li a[n];
for(li i=0;i<n;i++)
cin>>a[i];
mergeSort(a,0,n-1);
for(li i=0;i<n;i++)
cout<<temp[i]<<" ";
return 0;
}

Pls wait… let me go through your code…I will let u know the mistakes…

I have edited your code… Try to submit it now…

1 Like