void merge(vector<int>&arr,int l,int mid,int h)
{
vector<int>temp;
int i=l;
int j=mid+1;
while(i<=mid and j<=h)
{
if(arr[i]<=arr[j])
{
temp.push_back(arr[i]);
i++;
}
else
{
temp.push_back(arr[j]);
j++;
}
}
while(i<=mid)
{
temp.push_back(arr[i]);
i++;
}
while(j<=h)
{
temp.push_back(arr[j]);
j++;
}
for(int i=l ; i<=h ; i++)
{
arr[i]=temp[i-l];
}
}
void me(vector<int>&arr,int l,int h)
{
if(l>h)
return;
int mid=(l+h)/2;
me(arr,l,mid);
me(arr,mid+1,h);
merge(arr,l,mid,h);
}
void mergeSort(vector < int > & arr, int n)
{
me(arr,0,n-1);
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter