insertion sort(插入排序)
#include<stdio.h> #include<time.h> int insertion_sort() { const int max =10000; int a[max],i,j; srand((unsigned int)time(NULL)); for(i=0;i<max;i++) a[i]=rand(); for(i=1;i<max;i++) { j=i-1; int tmp=a[i]; while(j>=0&&tmp<a[j]) { a[j+1]=a[j]; j--; } a[j+1]=tmp; } i=0; while(i<max) printf("%d\t",a[i++]); return 0; }
MIT算法导论的第一节课小练习