void Merge(int *a, int s, int m, int e)
{
int lLen = m - s + 1;
int rLen = e - m;
int *l = new int[lLen];
int *r = new int[rLen];
int i = 0;
for(i; i < lLen; i++)
l[i] = a[s+i];
for(i = 0; i < rLen; i++)
r[i] = a[m+1+i];
int k;
int lK = 0;
int rK = 0;
for(k = s; k <= e; k++)
{
if(lK < lLen && rK < rLen)
{
if(l[lK] > r[rK])
a[k] = l[lK++];
else
a[k] = r[rK++];
}
else
{
if(lK >= lLen)
a[k] = r[rK++];
else if(rK >= rLen)
a[k] = l[lK++];
}
}
delete []l;
delete []r;
}
void MergeSort(int *a, int s, int e)
{
if(s < e)
{
int m = (s + e) / 2;
MergeSort(a, s, m);
MergeSort(a, m+1, e);
Merge(a, s, m, e);
}
}
合并排序
最新推荐文章于 2021-05-19 11:50:54 发布