插入排序

#include<stdio.h>

void InsertSort(int n,int a[])
{
    int j;
    for(int i=0;i<n;i++)
    {
        int tmp=a[i];
        for(j=i-1;j>=0;j--)
        {
            if(a[j]>tmp)
            {
                a[j+1]=a[j];
            }
            else break;
        }
        a[j+1]=tmp;
    }
}

int main()
{
    int a[100],n;
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=0; i<n; i++) scanf("%d",&a[i]);
        InsertSort(n,a);
        for(int i=0; i<n; i++) printf("%d ",a[i]);
        printf("
");
    }
    return 0;
}

  

原文地址:https://www.cnblogs.com/zsboy/p/3869706.html