版权声明:未经原作者允许不得转载本文内容,否则将视为侵权 https://blog.csdn.net/springhammer/article/details/88559130
主要功能:插入排序法.
#include <stdio.h>
#include <stdlib.h>
//插入排序
void InsertSort(int *a,int len);
//输出数组中的元素
void OutputArray(int *a, int len);
int main()
{
int a[10] = {2, 9, 5, 4, 8, 1, 6,11,3,20};
//输出数组中的元素
printf("排序前的数据:");
OutputArray(a,10);
//插入排序
InsertSort(a,10);
//输出排序后的结果
printf("排序后的数据:");
OutputArray(a,10);
system("pause");
}
//插入排序
void InsertSort(int *a,int len)
{
for(int i=1;i<len;i++)
{
int j=i-1;
int temp=a[i];//需要插入的数据
while(temp<a[j] && j>=0)//当插入的数据小于前面的数据时
{
a[j+1]=a[j];//将插入的数据的前面的数据向后移动
j--;
}
a[++j]=temp;//插入数据
}
}