#include
using namespace std;
void merge(int a[],int s,int e)
{
int mid=(s+e)/2;
int i=s;
int j=mid+1;
int k=s;
int temp[1000];
while(i<=mid && j<=e){
if(a[i]<a[j]){
temp[k++]=a[i++];
}
if(a[i]>=a[j]){
temp[k++]=a[j++];
}
}
while(i<=mid){
temp[k++]=a[i++];
}
while(j<=e){
temp[k++]=a[j++];
}
for(i=s;i<=e;i++)
{
a[i]=temp[i];
}
}
void mergeSort(int a[],int s,int e){
if(s==e)
{
return ;
}
int mid= (s+e)/2;
mergeSort(a,s,mid);
mergeSort(a,mid-1,e);
merge(a,s,e);
}
int main() {
int n,i,a[10000];
cin>>n;
for(i=0;i<n;i++)
{
cin>>a[i];
}
mergeSort(a,0,n-1);
for(i=0;i<n;i++)
{
cout<<a[i]<<β ';
}
return 0;
}
What am i doing wrong?
@shivansh.sm1,
Hello Shivansh,
please share your code using coding blocks ide.
a) paste your code at coding blocks ide
b) press save button
c) share the generated link here
@shivansh.sm1
here is your working code link - https://ide.codingblocks.com/s/178645
I have done some modifications in line no 31 and 37 .
pls have a look
@shivansh.sm1
ok got it,your implementation is correct.
your code is failing for large value of n .
as per given constraints

N can be upto 2*10^5 but the array size you declared is small so increase the size of your temp array and arr array to 3x10^5 .
it will work
