归并排序==>递归
#include "stdafx.h"
#include
void Run(int * pData,int nCount)
{
if (nCount > 1)
{
int k = nCount / 2;
Run(pData,k);
Run(pData + k,nCount - k);
int * pData1 = new int[nCount];
int * p1 = pData;
int * p2 = pData + k;
int * p3 = pData1;
while(p1 < pData + k && p2 < pData + nCount)
{
if (*p2 < * p1)
{
*p3++ = *p2++;
}
else
{
*p3++ = *p1++;
}
}
if (p1 < pData + k)
{
while (p1 < pData + k)
{
*p3++ = *p1++;
}
}
else if (p2 < pData + nCount)
{
while (p2 < pData + nCount)
{
*p3++ = *p2++;
}
}
memcpy(pData,pData1,nCount * sizeof(int));
delete []pData1;
}
}
int main()
{
int testArray[] = {1,10,8,9,5,3,4,5,7,4};
Run(testArray,sizeof(testArray) / sizeof(testArray[0]));
getchar();
return 0;
}
阅读(1526) | 评论(0) | 转发(0) |