void sortedListMerge(int* al1, int an1, int* al2, int an2, int* bl)
{
if (NULL == al1 || NULL == al2 || NULL == bl) {
return ;
}
int i1 = 0;
int i2 = 0;
int i3 = 0;
while (i1<an1 && i2<an2)
{
if (al1[i1] < al2[i2]) {
bl[i3++] = al1[i1++];
} else {
bl[i3++] = al2[i2++];
}
}
while (i1<an1) {
bl[i3++] = al1[i1++];
}
while (i2<an2) {
bl[i3++] = al2[i2++];
}
return ;
}
int mergeSort(int * aList, const int num)
{
if (NULL == aList) {
return -1;
}
if (2 > num) {
return 0;
}
int * bList = new (std::nothrow) int[num];
assert(NULL != bList);
int* al = aList;
int* bl = bList;
for (int step=1; step<num; step*=2) {
for (int i=0; i<num; i+=step*2) {
sortedListMerge(al+i, min(step, num - i), al+i+step, min(step, num - i - step), bl+i);
}
int* tmp = al;
al = bl;
bl = tmp;
}
if (al != aList) {
copyList(aList, al, num);
}
if (NULL != bList) {
delete [] bList;
bList = NULL;
}
return 0;
}