/*=======================================================
* 函 数 名: Swap(int *, int *)
* 参数说明: pa 指向要交换的位置
pb 指向要交换的位置
* 功能描述: 将 pa 和 pb 所指的元素交换
* 返 回 值: void
=======================================================*/
inline void Swap(int *pa, int *pb)
{
int temp; // 临时变量,用于交换的存储
temp = *pa;
*pa = *pb;
*pb = temp;
}
/*=======================================================
* 函 数 名: Insert_sort(int *, int )
* 参数说明: pArray 为传入的数组
iCount 为数组的个数
* 功能描述: 将 pArray 按照从大到小的顺序排序
* 返 回 值: void
=======================================================*/
void Insert_sort(int *const pArray, int iCount)
{
int *pCurrent = pArray; // pCurrent 指向当前位置
int *pInsert = pArray + 1; // pInsert 指向要插入数值的位置
if(*pCurrent < *pInsert)
{
Swap(pCurrent, pInsert); // 将第一个元素和第二个元素交换
}
pInsert++;
iCount -= 2;
while(iCount > 0)
{
pCurrent = pArray; // 每一次都从头开始找起
while(pCurrent != pInsert)
{
if(*pInsert > *pCurrent)
{
const int iTemp = *pInsert; // iTemp 存储将要插入元素的值
int *pTemp = pInsert; // pTemp 用来向前寻找
while(pTemp != pCurrent) // 从pCurrent开始后移一个元素
{
*pTemp = *(pTemp - 1);
pTemp--;
}
*pCurrent = iTemp; // 插入要插入的元素
}
else
{
pCurrent++;
}
}
pInsert++;
iCount--;
}
}