直接插入排序

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

void DirectInsertSort(int a[],int n) {

    int i,j,temp;

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

        if(a[i-1]>a[i]){
            temp = a[i];
            for(j=i-1;a[j]>temp&&j>=0;j--) {

                a[j+1] = a[j];
            }
            a[j+1] = temp;
        }

    }
}

int main()
{
    int i;

    int a[10] = {1,4,5,3,2,7,8,5,34,19};

    printf("排序后:");

    DirectInsertSort(a,10);

    for( i=0; i<10; i++) {
        printf("%d ",a[i]);
    }

    printf("
");

    return 0;
}
原文地址:https://www.cnblogs.com/ncuhwxiong/p/7445757.html