看题目:
为了求平均时间,我不得不循环3次求平均值,所以需要等上三倍的时间。
等得不耐烦,CPU没下过50%。
数据在文件中,因为有100000个,所以读取也会花一定时间,但是整个程序只分配了一个数组。
代码:
#include <stdio.h>
#include <time.h>
#include <algorithm>
#define PER_SECOND 1000//一秒
using namespace std;
int a[100000];//全局数组
const int n = 100000;//十万个数据
//函数声明
void ReadArray(char*filename);
double STLSort();
double InsertSort();
double ShellSort();
double BubbleSort();
double QuickSort();
double SelectSort();
//排序函数
void InSort(int *a,int n);
void ShSort(int *a,int n);
void BbSort(int *a,int n);
int Partition(int *a,int low,int high);
void QcSort(int *a,int low,int high);
void SeSort(int *a,int n);
void Print(int *a,int n);
int main(){
double t_sort=0,t_sum_in=0,t_sum_sh=0,t_sum_bb=0,t_sum_qc=0,t_sum_ss=0;
int cnt = 3;//求3次的平均值
while(cnt--){
t_sort += STLSort();
}
printf("STL排序:%lfms\n",t_sort/3);
cnt = 3;
while(cnt--){
t_sum_in += InsertSort();
}
printf("直接插入排序:%lfms\n",t_sum_in/3);
cnt = 3;
while(cnt--){
t_sum_sh += ShellSort();
}
printf("希尔排序:%lfms\n",t_sum_sh/3);
cnt = 3;
while(cnt--){
t_sum_bb += BubbleSort();
}
printf("冒泡排序:%lfms\n",t_sum_bb/3);
cnt = 3;
while(cnt--){
t_sum_qc += QuickSort();
}
printf("快速排序:%lfms\n",t_sum_qc/3);
cnt = 3;
while(cnt--){
t_sum_ss += SelectSort();
}
printf("选择排序:%lfms\n",t_sum_ss/3);
return 0;
}
//函数实现
//把数据读到数组a中
void ReadArray(char*filename){
FILE *fp;
if(!(fp = fopen(filename,"r"))){
printf("Cannot open file\n");
exit(0);
}
int i = 0;
while(!feof(fp)){
fscanf(fp,"%d\n",&a[i++]);
}
fclose(fp);
}
double STLSort(){
ReadArray("E://data.txt");
clock_t begin,end;
begin = clock();
sort(a,a+n);
end = clock();
return (double)(end - begin);
}
double InsertSort(){
ReadArray("E://data.txt");
clock_t begin,end;
begin = clock();
InSort(a,n);
end = clock();
return (double)(end - begin);
}
double ShellSort(){
ReadArray("E://data.txt");
clock_t begin,end;
begin = clock();
ShSort(a,n);
end = clock();
return (double)(end - begin);
}
double BubbleSort(){
ReadArray("E://data.txt");
clock_t begin,end;
begin = clock();
BbSort(a,n);
end = clock();
return (double)(end - begin);
}
double QuickSort(){
ReadArray("E://data.txt");
clock_t begin,end;
begin = clock();
QcSort(a,0,n-1);
end = clock();
return (double)(end - begin);
}
double SelectSort(){
ReadArray("E://data.txt");
clock_t begin,end;
begin = clock();
SeSort(a,n);
end = clock();
return (double)(end - begin);
}
//
void InSort(int *a,int n){
for(int i=1;i<n;i++){
if(a[i-1]>a[i]){
int x = a[i];
a[i] = a[i-1];
int j = 0;
for(j=i-2;a[j]>x && j>=0;j--){
a[j+1] = a[j];
}
a[j+1] = x;
}
}
}
void ShSort(int *a,int n){
for(int dk=n/2;dk>0;dk/=2){
for(int i=dk;i<n;i++){
if(a[i]<a[i-dk]){
int x = a[i];
int j = 0;
for(j=i-dk;j>=0&&a[j]>x;j-=dk){
a[j+dk] = a[j];
}
a[j+dk] = x;
}
}
}
}
void BbSort(int *a,int n){
int i = n-1;
while(i>0){
int k = 0;
for(int j=0;j<i;j++){
if(a[j+1]<a[j]){
swap(a[j],a[j+1]);
k = j;//交换的位置
}
}
i = k;
}
}
int Partition(int *a,int low,int high){
int x = a[low];
while(low<high){
while(low<high && a[high]>=x){
--high;
}
a[low] = a[high];
while(low<high && a[low]<=x){
++low;
}
a[high] = a[low];
}
a[low] = x;
return low;
}
void QcSort(int *a,int low,int high){
if(low<high){
int loc = Partition(a,low,high);
QcSort(a,low,loc-1);
QcSort(a,loc+1,high);
}
}
void SeSort(int *a,int n){
for(int i=0;i<n-1;i++){
int k = i;
for(int j=i+1;j<n;j++){
if(a[k]>a[j]){
k = j;
}
}
if(k!=i){
swap(a[i],a[k]);
}
}
}
运行结果:
可以看出来,STL也不是那么快,还没我自己写的快排快。
还有很多排序函数,没事再排来看看。。。