通俗思路是:一堆高矮不一的人排队,首先第一个人作为参照,第二个人与这个人比较,若第二个人比第一个人高则第一个人位置后移,第二个人站到第一个人的位置,然后第三个人与第二个人比较。。。
直接贴代码:
// Algorithm.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include
#include
#include
using namespace std;
#define ARRAYLEN 10 //数组个数
//Func:交换两个数的值
void Swap(int& nNumA, int& nNumB)
{
nNumA = nNumA^nNumB;
nNumB = nNumA^nNumB;
nNumA = nNumA^nNumB;
}
void DirectInsertSort(int* pArray, const int nLen)
{
for(int i = 1; i < nLen; ++i)
{
for(int j = i - 1; j >= 0; --j)
{
if(pArray[j] > pArray[j + 1])
{
Swap(pArray[j], pArray[j + 1]);
}
else
{
break; //0~j的数组已经有序 跳出循环
}
}
int *pNum = pArray;
cout<<"\n第" <![]()
1594

被折叠的 条评论
为什么被折叠?



