1 /* 使用冒泡排序返回一列随机数的降序,并且记录时间 2 */ 3 #include<stdio.h> 4 #include<time.h> 5 #include<stdlib.h> 6 #include<windows.h> 7 8 #define LEN 10000 //一列数的长度 9 #define RANGE 1000 //随机数范围0-100010 #define SLEEP_TIME 5000 //排序前的等待时间11 12 int * get_rand_nums();13 int * get_decrease_nums();14 15 int main(){16 17 int *p, i, start_time, end_time;18 19 start_time = time(NULL);20 21 p = get_decrease_nums();22 printf("\nSorted nums:\n");23 for(i = 0; i < LEN; i++){24 printf("%d, ", *(p + i));25 }26 27 end_time = time(NULL);28 29 printf("\nStarting time: %d\n", start_time);30 printf("Ending time: %d\n", end_time);31 printf("Total time used: %d S", end_time - start_time - SLEEP_TIME/1000);32 printf("\a\a\a");33 return 0;34 }35 36 /* 生成LEN个随机数,以数组形式返回 */37 int * get_rand_nums(){38 39 static int nums[LEN];40 41 srand((unsigned) time(NULL));42 int i;43 printf("Generate these nums:\n");44 for(i = 0;i < LEN;i++){45 nums[i] = rand() % RANGE;46 printf("%d, ", nums[i]);47 }48 49 return nums;50 }51 52 /* 使用冒泡排序返回一个递减的数组 */53 int * get_decrease_nums(){54 55 int *arr,i,j,temp;56 57 arr = get_rand_nums();58 printf("\nSorting in 5 Senconds.....");59 Sleep(SLEEP_TIME);60 61 for(i = 0; i < LEN - 1;i++){62 for(j = 0; j < (LEN - i - 1); j++){63 if(*(arr + j + 1) > *(arr + j)){64 temp = *(arr + j);65 *(arr + j) = *(arr + j + 1);66 *(arr + j + 1) = temp; 67 }68 }69 }70 71 return arr;72 }
生气是拿别人做错的事来惩罚自己