您的位置:首页 > 编程语言 > C语言/C++

直接插入排序(C语言)实现

2017-09-08 16:47 183 查看
//插入排序

#include <stdio.h>

//函数声明

void InsertSort(int *a,int n);//a为数组地址,n为数组元素的个数

int main()

{

int a[8]={6,7,3,5,9,5,3,10};

InsertSort(a,8);

printf("插入排序的结果是:\n");

int i;

for(i=0;i<8;i++)
printf("%d\n",a[i]);

return 0;

}

//方法实现

void InsertSort(int *a,int n)

{

int i,j,temp;

for(j=1;j<n;j++)
{

temp=a[j];

i=j-1;

while(i>=0&&a[i]>temp)
{

a[i+1]=a[i];

i--;

}

a[i+1]=temp;

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: