189 8069 5689

插入排序基础实现

插入排序是将插入数据与前面的排好序的数据比较然后将数据插入到指定位置时间复杂度为O(N^2)

在吴堡等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供网站制作、成都做网站 网站设计制作按需制作,公司网站建设,企业网站建设,成都品牌网站建设,营销型网站,外贸网站制作,吴堡网站建设费用合理。

大概排序流程为将一数组,首先取第二个数据向第一个数据插入排序,一直到第n个向n-1个数组插入排序

#include

using namespace std;

void InsertSort(int *a, int length)

{

if (a == NULL || length < +0)

{

return;

}

for(int i = 1; i < length; i++)

{

int index = i;

int tmp = a[i];

while (index)

{

if (tmp < a[index-1])   //插入数据和前面的数据比较,并将大的向后移动

{

a[index] = a[index - 1];

}

else

{

break;

}

--index;

}

if (tmp

{

a[index] = tmp;

}

}

}

void Test()

{

int a[] = { 3, 6, 2, 8, 1, 5, 9, 4, 7, 0 };

InsertSort(a, 10);

for (int i = 0; i < sizeof(a) / sizeof(a[0])-1; i++)

{

cout << a[i] << " ";

}

cout << endl;

}

int main()

{

Test();

return 0;

}  

附件:http://down.51cto.com/data/2367551

分享名称:插入排序基础实现
转载来于:http://cdxtjz.cn/article/piejcs.html

其他资讯