您的位置:首页 > 理论基础 > 数据结构算法

插入排序

2016-06-09 21:16 281 查看
# include <stdio.h>
/**
* 时间:2016年6月5日 11:27:44
* 内容:使用递归来实现排列组合
*/

void InsertionSort(int*,int);
void Info(int*,int);

int main(void)
{
int a[10]={1,9,5,6,7,2,4,3,0,8};
int length=10;
Info(a,length);
InsertionSort(a,length);
Info(a,length);

return 0;
}
/**
* 插入排序
* @param a 未排序的数组
* @param n 数组长度
*/
void InsertionSort(int* a,int n)
{
int in,out,temp;

for(int out=1;out<n;out++)
{
temp=a[out];
in=out;
while(in>0&&a[in-1]>=temp)
{
a[in]=a[in-1];
in--;
}
a[in]=temp;
}
}
void Info(int*a,int n)
{
for (int i=0;i<n;i++)
{
printf("%d ",a[i]);
}
printf("\n");
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息