#include "time.h"
#include "stdlib.h"
#include "stdio.h"
void insertSort(int a[]){
int j ,i,key ;
clock_t start;
for(j = 1 ; j < 100000 ;j++){
start = clock(); //记录开始时间 //返回从系统启动到现在经过的毫秒数
key = a[j];
i = j - 1;
while( i >= 0 && a[i] > key){
a[ i + 1 ] = a[i];
i-=1;
a[i+1]=key;
}
}
clock_t finish = clock(); //结束时间
printf("reult ");
for(i = 0 ; i < 100000 ; i++){
printf(" %d " , a[i] );
if(i>0 && i % 10 ==0)
printf("\n");
}
double duration = (double)(finish - start) / CLOCKS_PER_SEC;
printf("/n %lf " ,duration);
}
void main( )
{
int i;
int a[100000];
srand( time(NULL) ); //生成种子
printf("start: ");
for(i=0;i<100000;i++)
{
a[i]=rand()%100000; //生成一个小于100000的随机数
printf(" %d " , a[i] );
if(i>0 && i % 10 ==0)
printf("\n");
}
printf("\n");
printf("\n");
insertSort(a);
}
插入排序算法
最新推荐文章于 2024-07-14 22:00:56 发布