插入排序
insertSort.c
test_insertSort1.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <sys/time.h>
4 #define MAXNUM 1000000
5 void insertSort(int [],int);
6
7 int main()
8 {
9 int i;
10 int j;
11 int num[MAXNUM];
12 struct timeval time1;
13 struct timeval time2;
14 long int runtime;
15 printf("Please enter a Integer:\n");
16 scanf("%d",&i);
17 printf("i is %d\n", i);
18 if(i > MAXNUM)
19 {
20 printf("You should input small number\n");
21 exit(0);
22 }
23 printf("1");
24 for(j = 0; j < i;j++)
25 {
26 num[j] = rand();
27 printf("%d\n", num[j]);
28 }
29 printf("running\n");
30 if(gettimeofday(&time1,NULL) == -1)
31 {
32 printf("you get time time1 error\n");
33 exit(0);
34 }
35 insertSort(num,i);
36 if(gettimeofday(&time2,NULL) == -1)
37 {
38 printf("you get time time2 error\n");
39 exit(0);
40 }
41 for(j = 0; j < i ; j++)
42 {
43 printf("%d\t", num[j]);
44 }
45 runtime = (time2.tv_sec - time1.tv_sec)*100000 +(time2.tv_usec - time1.tv_usec);
46 printf("The running is %ld microseconds\n", runtime);
47 return 0;
48
49 }
2 #include <stdlib.h>
3 #include <sys/time.h>
4 #define MAXNUM 1000000
5 void insertSort(int [],int);
6
7 int main()
8 {
9 int i;
10 int j;
11 int num[MAXNUM];
12 struct timeval time1;
13 struct timeval time2;
14 long int runtime;
15 printf("Please enter a Integer:\n");
16 scanf("%d",&i);
17 printf("i is %d\n", i);
18 if(i > MAXNUM)
19 {
20 printf("You should input small number\n");
21 exit(0);
22 }
23 printf("1");
24 for(j = 0; j < i;j++)
25 {
26 num[j] = rand();
27 printf("%d\n", num[j]);
28 }
29 printf("running\n");
30 if(gettimeofday(&time1,NULL) == -1)
31 {
32 printf("you get time time1 error\n");
33 exit(0);
34 }
35 insertSort(num,i);
36 if(gettimeofday(&time2,NULL) == -1)
37 {
38 printf("you get time time2 error\n");
39 exit(0);
40 }
41 for(j = 0; j < i ; j++)
42 {
43 printf("%d\t", num[j]);
44 }
45 runtime = (time2.tv_sec - time1.tv_sec)*100000 +(time2.tv_usec - time1.tv_usec);
46 printf("The running is %ld microseconds\n", runtime);
47 return 0;
48
49 }