// InsertSort.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include<iostream>
void InsertSort(int a[],int n)
{
int i,j;
int temp;
for (i=1;i<n;i++)
{
temp=a[i];//取出第一个准备插入的数字
for(j=i-1;j>=0 && temp<a[j];j--)
{
a[j+1]=a[j];
}
a[j+1]=temp;//插入数据到序列
}
}
int _tmain(int argc, _TCHAR* argv[])
{
int a[]={2,3,1,5};
InsertSort(a,4);
for (int i=0;i<4;i++)
{
printf("%3d ",a[i]);
}
system("pause");
return 0;
}
直接插入排序
最新推荐文章于 2025-08-10 08:58:43 发布