各种排序算法时间效率,数据移动次数以及比较次数对比【数据结构】

#include<bits/stdc++.h>
using namespace std;
#define MAXN 100000000
int maxn;
int N[MAXN+5];
//        Compare 比较次数          Move 移动次数 
long long InsertMove,InsertComp,BInsertMove,BInsertComp,QComp,QMove,BubbleComp,BubbleMove,ShellComp,ShellMove;

typedef struct{
    int key;
    //int m;
}ElemType;

typedef struct{
    int len;
    ElemType *r;//*r 与 r[MAXNSIZE]有区别
}sq;

void CreateNum(){
    srand((unsigned)time(NULL));
    for(int i=1;i<=maxn;i++){
        N[i]=rand()%MAXN;
    }
}

sq init(){
    sq L;
    L.len=maxn;
    L.r=new ElemType[maxn+1];
    for(int i=1;i<=maxn;i++){
        L.r[i].key=N[i];
    }
    return L;
}

void InsertSort(sq &L){
    int j;
    for(int i=2;i<=L.len;i++){
    	InsertComp++;
        if(L.r[i].key<L.r[i-1].key){
            L.r[0].key=L.r[i].key;
            InsertMove++;
            for(j=i-1;L.r[j].key>L.r[0].key;j--,InsertComp++){
                L.r[j+1].key=L.r[j].key;
                InsertMove++;
            }
            L.r[j+1].key=L.r[0].key;
            InsertMove++;
        }
    }
}

void BInsertSort(sq &L){
    int j,l,r,temp;
    for(int i=2;i<=L.len;i++){
        temp=L.r[i].key;
        l=1,r=i-1;
        while(l<=r){
            int m=(l+r)/2;
            if(temp<L.r[m].key) r=m-1;
            else l=m+1;
            BInsertComp++;
        }
        for(j=i;j>=l+1;j--) {
			L.r[j].key=L.r[j-1].key;//L.r[j]=L.r[j-1];
			BInsertMove++;
		}
        L.r[l].key=temp;
        BInsertMove++;
    }
}

void ShellInsert(sq &L,int dk){
	int i,j;
	for(i=dk+1;i<=L.len;i++){
		ShellComp++;
		if(L.r[i].key<L.r[i-dk].key){
			L.r[0]=L.r[i];
			ShellMove++;
			for(j=i-dk;j>0&&L.r[0].key<L.r[j].key;j-=dk,ShellComp++){
				L.r[j+dk]=L.r[j];
				ShellMove++;
			}		
			L.r[j+dk]=L.r[0];
			ShellMove++;
		}
	}
}

void ShellSort(sq &L,int t){//5 3 1 -> t=3   7 5 3 1 -> t=4
	int dt[100];
	for(int j=0;j<t;j++){ 
	/*
	3 0 5 5
	3 1 3 5
	3 2 1 5
	*/
		dt[j]=-2*j+t+2;
	}
 	for(int i=0;i<t;i++)
 		ShellInsert(L,dt[i]);
}

int Partition(sq &L,int low,int high){
	//对顺序表L中的子表r[low..high]进行一趟排序,返回枢轴位置
	int pivotkey;
	L.r[0]=L.r[low];                    	//用子表的第一个记录做枢轴记录
	pivotkey=L.r[low].key;		   			//枢轴记录关键字保存在pivotkey中
	while(low<high)	{										//从表的两端交替地向中间扫描
		while(low<high&&L.r[high].key>=pivotkey) --high,QComp++;
		L.r[low]=L.r[high],QMove++;				//将比枢轴记录小的记录移到低端
		while(low<high&&L.r[low].key<=pivotkey) ++low,QComp++;
		L.r[high]=L.r[low],QMove++;					//将比枢轴记录大的记录移到高端
		
	}//while
	L.r[low]=L.r[0];						//枢轴记录到位
	return  low;							//返回枢轴位置
}//Partition

void QSort(sq &L,int low,int high){	//调用前置初值:low=1; high=L.length;
    //对顺序表L中的子序列L.r[low..high]做快速排序
	int pivotloc;
    if(low<high){										//长度大于1
       pivotloc=Partition(L,low,high); 		//将L.r[low..high]一分为二,pivotloc是枢轴位置
       QSort(L,low,pivotloc-1);				//对左子表递归排序
       QSort(L,pivotloc+1,high);        	//对右子表递归排序
	}
}											//QSort

void QuickSort(sq &L){
	//对顺序表L做快速排序
   QSort(L,1,L.len);
}											//QuickSort

void BubbleSort(sq &L){
	for(int i=1;i<L.len;i++){
		for(int j=1;j<=L.len-i;j++){
			BubbleComp++;
			if(L.r[j+1].key<L.r[j].key) {
				swap(L.r[j+1],L.r[j]);
				BubbleMove+=2;
			}
		}
	}	
}

void show(sq L){
    for(int i=1;i<=L.len;i++)
        cout<<L.r[i].key<<' ';
    puts("");
}

int main(){
	int temp=100;
    for(int i=1;i<=5;i++){//100~1000000
    	InsertMove=0,InsertComp=0,BInsertMove=0,BInsertComp=0,QComp=0,QMove=0,BubbleComp=0,BubbleMove=0,ShellComp=0,ShellMove=0; 
    	maxn=temp;
    	cout<<"当前数量级是"<<temp<<endl;  
    	CreateNum();//生成一组随机数
    	sq L=init();
        double s=clock();
        InsertSort(L);
        double e=clock();
        cout<<"直接插入排序时间是"<<e-s<<"ms"<<endl;
        cout<<"直接插入移动次数是"<<InsertMove<<endl;
        cout<<"直接插入比较次数是"<<InsertComp<<endl<<endl;
        
        L=init();
        s=clock();
        BInsertSort(L);
        e=clock();
        cout<<"二分插入排序时间是"<<e-s<<"ms"<<endl;
        cout<<"二分插入移动次数是"<<BInsertMove<<endl;
        cout<<"二分插入比较次数是"<<BInsertComp<<endl<<endl;
        
        L=init();
        s=clock();
        QuickSort(L);
        e=clock();
        cout<<"快排时间是"<<e-s<<"ms"<<endl;
        cout<<"快排移动次数是"<<QMove<<endl;
        cout<<"快排比较次数是"<<QComp<<endl<<endl;
        
        L=init();
    	s=clock();
		ShellSort(L,3);// dt 5 3 1 -> 3
        e=clock();
        cout<<"希尔排序时间是"<<e-s<<"ms"<<endl;
        cout<<"希尔排序移动次数是"<<ShellMove<<endl;
        cout<<"希尔排序比较次数是"<<ShellComp<<endl<<endl;
        
        L=init();
    	s=clock();
        BubbleSort(L);
        e=clock();
        cout<<"冒泡排序时间是"<<e-s<<"ms"<<endl;
        cout<<"冒泡排序移动次数是"<<BubbleMove<<endl;
        cout<<"冒泡排序比较次数是"<<BubbleComp<<endl<<endl;
        
        temp*=10;
        cout<<"------------------------------------------"<<endl;
	}
    return 0;
}

 结果输出到文件中

#include<bits/stdc++.h>
using namespace std;
#define MAXN 100000000
int maxn;
int N[MAXN+5];
//        Compare 比较次数          Move 移动次数
long long InsertMove,InsertComp,BInsertMove,BInsertComp,QComp,QMove,BubbleComp,BubbleMove,ShellComp,ShellMove;
 
typedef struct{
    int key;
    //int m;
}ElemType;
 
typedef struct{
    int len;
    ElemType *r;//*r 与 r[MAXNSIZE]有区别
}sq;
 
void CreateNum(){
    srand((unsigned)time(NULL));
    for(int i=1;i<=maxn;i++){
        N[i]=rand()%MAXN;
    }
}
 
sq init(){
    sq L;
    L.len=maxn;
    L.r=new ElemType[maxn+1];
    for(int i=1;i<=maxn;i++){
        L.r[i].key=N[i];
    }
    return L;
}
 
void InsertSort(sq &L){
    int j;
    for(int i=2;i<=L.len;i++){
    	InsertComp++;
        if(L.r[i].key<L.r[i-1].key){
            L.r[0].key=L.r[i].key;
            InsertMove++;
            for(j=i-1;L.r[j].key>L.r[0].key;j--,InsertComp++){
                L.r[j+1].key=L.r[j].key;
                InsertMove++;
            }
            L.r[j+1].key=L.r[0].key;
            InsertMove++;
        }
    }
}
 
void BInsertSort(sq &L){
    int j,l,r,temp;
    for(int i=2;i<=L.len;i++){
        temp=L.r[i].key;
        l=1,r=i-1;
        while(l<=r){
            int m=(l+r)/2;
            if(temp<L.r[m].key) r=m-1;
            else l=m+1;
            BInsertComp++;
        }
        for(j=i;j>=l+1;j--) {
			L.r[j].key=L.r[j-1].key;//L.r[j]=L.r[j-1];
			BInsertMove++;
		}
        L.r[l].key=temp;
        BInsertMove++;
    }
}
 
void ShellInsert(sq &L,int dk){
	int i,j;
	for(i=dk+1;i<=L.len;i++){
		ShellComp++;
		if(L.r[i].key<L.r[i-dk].key){
			L.r[0]=L.r[i];
			ShellMove++;
			for(j=i-dk;j>0&&L.r[0].key<L.r[j].key;j-=dk,ShellComp++){
				L.r[j+dk]=L.r[j];
				ShellMove++;
			}
			L.r[j+dk]=L.r[0];
			ShellMove++;
		}
	}
}
 
void ShellSort(sq &L,int t){//5 3 1 -> t=3   7 5 3 1 -> t=4
	int dt[100];
	for(int j=0;j<t;j++){
	/*
	3 0 5 5
	3 1 3 5
	3 2 1 5
	*/
		dt[j]=-2*j+t+2;
	}
 	for(int i=0;i<t;i++)
 		ShellInsert(L,dt[i]);
}
 
int Partition(sq &L,int low,int high){
	//对顺序表L中的子表r[low..high]进行一趟排序,返回枢轴位置
	int pivotkey;
	L.r[0]=L.r[low];                    	//用子表的第一个记录做枢轴记录
	pivotkey=L.r[low].key;		   			//枢轴记录关键字保存在pivotkey中
	while(low<high)	{										//从表的两端交替地向中间扫描
		while(low<high&&L.r[high].key>=pivotkey) --high,QComp++;
		L.r[low]=L.r[high],QMove++;				//将比枢轴记录小的记录移到低端
		while(low<high&&L.r[low].key<=pivotkey) ++low,QComp++;
		L.r[high]=L.r[low],QMove++;					//将比枢轴记录大的记录移到高端
 
	}//while
	L.r[low]=L.r[0];						//枢轴记录到位
	return  low;							//返回枢轴位置
}//Partition
 
void QSort(sq &L,int low,int high){	//调用前置初值:low=1; high=L.length;
    //对顺序表L中的子序列L.r[low..high]做快速排序
	int pivotloc;
    if(low<high){										//长度大于1
       pivotloc=Partition(L,low,high); 		//将L.r[low..high]一分为二,pivotloc是枢轴位置
       QSort(L,low,pivotloc-1);				//对左子表递归排序
       QSort(L,pivotloc+1,high);        	//对右子表递归排序
	}
}											//QSort
 
void QuickSort(sq &L){
	//对顺序表L做快速排序
   QSort(L,1,L.len);
}											//QuickSort
 
void BubbleSort(sq &L){
	for(int i=1;i<L.len;i++){
		for(int j=1;j<=L.len-i;j++){
			BubbleComp++;
			if(L.r[j+1].key<L.r[j].key) {
				swap(L.r[j+1],L.r[j]);
				BubbleMove+=2;
			}
		}
	}
}
 
void show(sq L){
    for(int i=1;i<=L.len;i++)
        cout<<L.r[i].key<<' ';
    puts("");
}
 
int main(){
    ofstream fp;
    //fp.open("C:\\Users\\Answer\\Desktop\\排序算法数据统计.txt",ios_base::app);
    fp.open("C:\\Users\\Answer\\Desktop\\排序算法数据统计.txt");
 
	int temp=100;
    for(int i=1;i<=5;i++){//100~1000000
    	InsertMove=0,InsertComp=0,BInsertMove=0,BInsertComp=0,QComp=0,QMove=0,BubbleComp=0,BubbleMove=0,ShellComp=0,ShellMove=0;
    	maxn=temp;
    	fp<<"当前数量级是"<<temp<<endl;
    	CreateNum();//生成一组随机数
    	sq L=init();
        double s=clock();
        InsertSort(L);
        double e=clock();
        fp<<"直接插入排序时间是"<<e-s<<"ms"<<endl;
        fp<<"直接插入移动次数是"<<InsertMove<<endl;
        fp<<"直接插入比较次数是"<<InsertComp<<endl<<endl;
 
        L=init();
        s=clock();
        BInsertSort(L);
        e=clock();
        fp<<"二分插入排序时间是"<<e-s<<"ms"<<endl;
        fp<<"二分插入移动次数是"<<BInsertMove<<endl;
        fp<<"二分插入比较次数是"<<BInsertComp<<endl<<endl;
 
        L=init();
        s=clock();
        QuickSort(L);
        e=clock();
        fp<<"快排时间是"<<e-s<<"ms"<<endl;
        fp<<"快排移动次数是"<<QMove<<endl;
        fp<<"快排比较次数是"<<QComp<<endl<<endl;
 
        L=init();
    	s=clock();
		ShellSort(L,3);// dt 5 3 1 -> 3
        e=clock();
        fp<<"希尔排序时间是"<<e-s<<"ms"<<endl;
        fp<<"希尔排序移动次数是"<<ShellMove<<endl;
        fp<<"希尔排序比较次数是"<<ShellComp<<endl<<endl;
 
        L=init();
    	s=clock();
        BubbleSort(L);
        e=clock();
        fp<<"冒泡排序时间是"<<e-s<<"ms"<<endl;
        fp<<"冒泡排序移动次数是"<<BubbleMove<<endl;
        fp<<"冒泡排序比较次数是"<<BubbleComp<<endl<<endl;
 
        temp*=10;
        fp<<"------------------------------------------"<<endl;
	}
    return 0;
}
 

猜你喜欢

转载自blog.csdn.net/qq_41333844/article/details/84728307
今日推荐