插入排序

/*
**插入排序
*/
#include<stdio.h>
void InsertSort(int A[],int N)
{
    int i,j;
    for(i=1;i<N;i++)
    {
        int tmp=A[i];
        for(j=i-1;j>=0&&tmp<A[j];j--)
        {
            A[j+1]=A[j];
        }
        A[j+1]=tmp;/*tmp>A[j],所以tmp存储在A[j+1]的位置*/
    }
}
void PrintfArray(int A[],int N)
{
    int i;
    for(i=0;i<N;i++)
    {
        printf("%d
",A[i]);
    }
}
void ScanfArray(int A[],int N)
{
    int i;
    for(i=0;i<N;i++)
    {
        scanf("%d",&A[i]);
    }
}
int main()
{
    int N;
    scanf("%d",&N);
    int A[N];
    ScanfArray(A,N);
    InsertSort(A,N);
    PrintfArray(A,N);
    return 0;
}
原文地址:https://www.cnblogs.com/angury/p/12990015.html