直接插入排序

#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("\n");

    return 0;
}

 

posted @ 2017-08-28 21:06  胡卫雄  阅读(141)  评论(0编辑  收藏  举报