#include <stdio.h> void InsertSort( int *a, int n ) { int i, j; int temp; for( i=1; i<n; i++ ) { //取出第i个记录的关键字 temp = a[i]; for( j=i-1; j>=0; j-- ) { if( temp < a[j] ) { if( 0 == j ) { //取出的关键字比当前有序区的第一个关键字还要小的情况,做特殊处理(也可以利用哨兵来处理) a[j+1] = a[j]; a[j] = temp; } else a[j+1] = a[j]; } else { a[j+1] = temp; break; } } } } int main() { int i; int a[10] = {7,4,1,8,5,2,9,6,3,0}; InsertSort( a, 10 ); for( i=0; i<10; i++ ) printf("%d ", a[i]); printf("\n"); return 0; }