#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
//定义一个断言,用来处理异常处理
#define ASSERT(condition,error_message)\
{\
if(!(condition))\
{\
printf("%s\n",error_message);\
goto exception_state;\
}\
}
#define swap(x,y) {int t=x;x=y;y=t;}
//有关某个算法的相关信息。
struct sSort
{
void (*sort)(int a[],unsigned int size);//算法实现的函数名
char name[128];//算法名字,不超过128个字符
bool isContinue;//继续算法比较
};
bool ascending_order(int a[],unsigned int size);//升序的判定。
void bubble_sort(int a[],unsigned int size);//冒泡排序
void copy_array(int source[],int target[],unsigned int size);//数组复制
void init_array(int a[],unsigned int size);//产生随机数据
void print_array(int a[],unsigned int size);//输出数组
void quick_sort(int a[],unsigned int size);//快速排序
double run_sort(void (*sort)(int a[],unsigned int size),int a[],unsigned int size);//运行排序算法sort,返回运行时间
void select_sort(int a[],unsigned int size);//选择排序。
//数据池
int sampled[10000000],a[10000000];
//各种排序算法信息
struct sSort sortSet[]={
{select_sort,"选择排序",true},
{bubble_sort,"冒泡排序",true},
{quick_sort,"快速排序",true}
};
int main(int argc, char** argv)
{
int sort_set_size=sizeof(sortSet)/sizeof(sortSet[0]);//排序算法的个数
srand(time(0));
printf("%10s","规模");
int i;
for(i=0;i<sort_set_size;i++)
printf("%10s",sortSet[i].name);
printf("\n");
int size;
for(size=100;size<=10000000;size*=10)
{
double run_time;
double sum_run_time[sort_set_size];//排序运行若干次的总时间
int i;
for(i=0;i<sort_set_size;i++)
sum_run_time[i]=0;
const int test_size=10;//定义每个规模的测试次数
printf("%10d",size);
int count;
for(count=0;count<test_size;count++) //产生十次,获取平均值。
{
init_array(sampled,size);
int i;
for(i=0;i<sort_set_size;i++)
{
if(!sortSet[i].isContinue) continue; //如果太长时间了,
copy_array(sampled,a,size);
sum_run_time[i]+=run_sort(sortSet[i].sort,a,size);
char error_message[128];
sprintf(error_message,"%s算法有误!",sortSet[i].name);
ASSERT(ascending_order(a,100),error_message);
}
}
for(i=0;i<sort_set_size;i++) //输出平均时间,如果超过1秒,则后面的实验就不继续了。
if(sortSet[i].isContinue)
{
printf("%10.4f",sum_run_time[i]/test_size);
if(sum_run_time[i]/test_size>1)
sortSet[i].isContinue=false;
}
else
printf(" -- ");
printf("\n");
}
return 0;
exception_state:
printf("程序被强行终止,请修改后,再次运行!\n");
return 1;
}
void init_array(int a[],unsigned int size)
{
for(int i = 0; i < size-1; i++)
{
a[i] = rand();
}
}
void copy_array(int source[],int target[],unsigned int size)
{
for(int i = 0; i<size-1; i++ )
{
target[i] = source[i];
}
}
void select_sort(int a[],unsigned int size)
{
for (int i = 0; i < size; i++)
{
int min=i;
for(int j = i+1; j<size; j++)
{
if (a[j]<a[min])
{
min =j;
}
}
int k = a[min];
a[min] = a[i];
a[i] =k;
}
}
void bubble_sort(int a[],unsigned int size)
{
for(int i = 0,j,temp; i<size-1; i++ )
{
for(j=0; j<size-1-i;j++)
{
if (a[j]>a[j+1])
{
temp = a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
}
void quick_sort(int a[],unsigned int size)
{
int l=0,r=size-1;
if(l < r)
{
int i, j, x;
i = l;
j = r;
x = a[i];
while(i < j)
{
while(i < j && a[j] > x)
j--;
if(i < j)
a[i++] = a[j];
while(i < j && a[i] < x)
i++;
if(i < j)
a[j--] = a[i];
}
a[i] = x;
quick_sort(a,i);
quick_sort(a+i+1,size-1-i);
}
}
bool ascending_order(int a[],unsigned int size)
{
for(int i =size-1; i>0;i--)
{
if(a[i]<a[i-1])
return false;
}
return true;
}
void print_array(int a[],unsigned int size)
{
for(int i = 0; i<size; i++)
{
printf("%d ",a[i]);
}
printf("\n");
}
double run_sort(void (*sort)(int a[],unsigned int size),int a[],unsigned int size)
{
double clock_1 = clock();
(*sort)(a,size);
double clock_2 = clock();
return (clock_2-clock_1)/CLOCKS_PER_SEC;
}