本篇並非是講述具體的排序算法,而是做一些準備工作
1. 生成10000個隨機數,作爲後續測試排序算法的數據源。
/* 生成10000個可重複的隨機數,保存到文件中,並計算生成時間 */
#include <stdio.h>
#include <stdlib.h>
#include <ctime>
typedef long clock_t;
void main()
{
clock_t start_time = clock(); // 計時開始
srand(unsigned(time(0))); // 生成時間種子,srand()和rand()函數頭文件是<stdlib.h>。
// rand()生成的是僞隨機數,srand()種子不同,rand()生成的隨機數列也不同,生成的隨機數相同的概率就小了。
// 連續兩個rand()rand()生成的隨機數是一樣的。
// rand()默認生成0~RAND_MAX(在stdlib.h中定義,默認大小爲32767)之間的一個數。
const int MAX = 10000;
const int MIN = 1;
// 將標準輸出流定位到文件中
if((freopen("data.txt", "w", stdout)) == NULL)
exit(-1);
for(int i = 0; i < 10000; i++)
{
unsigned int data = rand();
printf("%d ", data);
}
// 將標準輸出流定位到控制檯
if((freopen("CON", "w", stdout)) == NULL)
exit(-1);
clock_t stop_time = clock();
printf("Elapsed time: %dms\n", (stop_time - start_time));
}
2. 打印數組元素
#include "Print.h"
// 輸出每一步的排序效果
void Print(int A[], int N)
{
for(int i = 0; i < N; i++)
{
printf("%d ", A[i]);
}
printf("\n");
}