插入排序

#include<stdio.h>
#include<stdlib.h>

//函数声明
void InsertSort(int a[], int n); //插入排序(从小到大)

int main()
{
int i, n, a[100];
printf("请输入需要排序元素的个数:");
scanf("%d", &n);
printf("随机生成的数组为:");
for (i = 1; i <= n; i++)
{
a[i] = rand() % 100 + 1;
printf("%d ", a[i]);
}
a[i] = '';
printf(" ");
InsertSort(a, n);
printf(" 插入排序结果为(由小到大):");
for (i = 1; i <= n; i++)
printf("%d ", a[i]);

}

//插入排序(从小到大)
void InsertSort(int a[], int n)
{
int temp;

for(int i=1;i<n;i++)
{
temp=a[i];
for(int j=i-1;j>0&&temp<a[j];j--)
a[j+1] =a[j];
a[j+1]=temp;
}
}

原文地址:https://www.cnblogs.com/qin5429/p/8372443.html