数据结构《多种排序算法性能比较》课程设计

/*
在定时前应该先调用QueryPerformanceFrequency()函数
获得机器内部计时器的时钟频率。
接着在需要严格计时的事件发生前和发生之后
分别调用QueryPerformanceCounter(),
利用两次获得的计数之差和时钟频率,
就可以计算出事件经历的精确时间。
*/

#include<iostream>
#include <fstream>
#include<windows.h>
#include <stdio.h>
#include <windows.h>
#include <ctime>
#include<cstdlib> 
using namespace std;
typedef int KeyType;//定义关键字类型为int
typedef int InfoType;
typedef struct    //元素类型 
{
	KeyType key;  //关键字项 
	InfoType data;  //其他元素项
 }RecType;//排序元素的类型 
//直接插入排序
void InsertSort(RecType R[],int n)
//对R[0,n-1]进行有序直接插入排序 
{
	int i,j;
	RecType tmp;
	for(i=1;i<n;i++)
	{
		if(R[i].key<R[i-1].key)//反序时 
		{
			tmp=R[i];
			j=i-1;
			do  //找R[i]的插入位置 
			{
				R[j+1]=R[j];//将关键字大于R[j].key的关键字后移 
				j--;					
			 }while(j>=0 && R[j].key>tmp.key);
			 R[j+1]=tmp;//在j+1处插入R[j]	
		}	
	 } 
 } 
 void DispList(RecType R[],int n)	//输出顺序表
{
	for (int i=0;i<n;i++)
		printf("%d ",R[i].key);
	printf("\n");
}
//折半插入排序 
void BinInsertSort(RecType R[],int n)
{
	int i,j,low,high,mid;
	RecType tmp;
	for(i=1;i<n;i++)
	{
		if(R[i].key<R[i-1].key)//反序时
		{
		tmp=R[i];
		low=0;
		high=i-1;
		while(low<=high)
		//在R[low,high]中查找插入的位置 
		{
		mid=(low+high)/2;//取中间位置
		if(tmp.key<R[mid].key)
		high=mid-1;//插入在左边区
		else
		low=mid+1;//插入在右半区	
		 } //找位置high 
		 for(j=i-1;j>=high+1;j--)//对元素进行元素后移 
			R[j+1]=R[j];
		R[high+1]=tmp;		
		}		
}
 } 

//冒泡排序算法
void BubbleSort(RecType R[],int n)
{
	int i,j,k;
	RecType tmp;
	for (i=0;i<n-1;i++) 
	{
		for (j=n-1;j>i;j--)	//比较,找出本趟最小关键字的记录
			if (R[j].key<R[j-1].key)   
			{
				tmp=R[j];  //R[j]与R[j-1]进行交换,将最小关键字记录前移
				R[j]=R[j-1];
				R[j-1]=tmp;
			}
	//	printf("  i=%d: ",i);
	//	DispList(R,n);
	}
} 
//快速排序算法 
int partition(RecType R[],int s,int t)	//一趟划分
{   int count=0;
	int i=s,j=t;
	RecType tmp=R[i];			//以R[i]为基准
	while (i<j)  				//从两端交替向中间扫描,直至i=j为止
	{	while (j>i && R[j].key>=tmp.key)
			j--;				//从右向左扫描,找一个小于tmp.key的R[j]
		R[i]=R[j];				//找到这样的R[j],放入R[i]处
		while (i<j && R[i].key<=tmp.key)
			i++;				//从左向右扫描,找一个大于tmp.key的R[i]
		R[j]=R[i];				//找到这样的R[i],放入R[j]处
	}
	R[i]=tmp;
	return i;
}
//快速排序算法1
int count=0; 
void QuickSort(RecType R[],int s,int t) 
//对R[s..t]的元素进行快速排序
{	int i;
	RecType tmp;
	if (s<t) 					//区间内至少存在两个元素的情况
	{	count++;
		i=partition(R,s,t);
		//DispList(R,10);			//调试用
		QuickSort(R,s,i-1);		//对左区间递归排序
		QuickSort(R,i+1,t);		//对右区间递归排序
	}
}
//简单选择排序算法

void SelectSort(RecType R[],int n)
{
	int i,j,k;
	RecType temp;
	for (i=0;i<n-1;i++)    	 	//做第i趟排序
	{
		k=i;
		for (j=i+1;j<n;j++)  	//在当前无序区R[i..n-1]中选key最小的R[k]
			if (R[j].key<R[k].key)
				k=j;           	//k记下目前找到的最小关键字所在的位置
			if (k!=i)          		//交换R[i]和R[k]
			{
				temp=R[i];
				R[i]=R[k];
				R[k]=temp;  
			}
		//printf("  i=%d: ",i);
		// DispList(R,n);
	}
}
//希尔排序算法 
void ShellSort(RecType R[],int n) //希尔排序算法
{	int i,j,d;
	RecType tmp;
	d=n/2;					//增量置初值
	while (d>0)
	{	for (i=d;i<n;i++) 	//对所有组采用直接插入排序
		{	tmp=R[i];		//对相隔d个位置一组采用直接插入排序
			j=i-d;
			while (j>=0 && tmp.key<R[j].key)
			{	R[j+d]=R[j];
				j=j-d;
			}
			R[j+d]=tmp;
		}
		//printf("  d=%d: ",d); 
		//DispList(R,n);
		d=d/2;				//减小增量
	}
}
//
/*
	在定时前应该先调用QueryPerformanceFrequency()函数
	获得机器内部计时器的时钟频率。
	接着在需要严格计时的事件发生前和发生之后
	分别调用QueryPerformanceCounter(),
	利用两次获得的计数之差和时钟频率,
	就可以计算出事件经历的精确时间。
*/
直接插入排序时间分析函数 

void timeinsert(RecType R[],int n)
{
	double time=0;
    double counts=0;
    LARGE_INTEGER nFreq;
    LARGE_INTEGER nBeginTime;
    LARGE_INTEGER nEndTime;
    QueryPerformanceFrequency(&nFreq);
    QueryPerformanceCounter(&nBeginTime);//开始计时
        //...测试代码
        InsertSort(R,n);
    QueryPerformanceCounter(&nEndTime);//停止计时
    time=(double)(nEndTime.QuadPart-nBeginTime.QuadPart)/(double)nFreq.QuadPart;//计算程序执行时间单位为s
    //cout<<"运行时间:"<<time*1000<<"ms"<<endl;
    printf("直接插入排序spend time: %f ms\n",(time *1000));
	//return time;
}
	//冒泡排序算法时间函数 
void timeBubbleSort(RecType R[],int n)
{
	double time=0;
    double counts=0;
    LARGE_INTEGER nFreq;
    LARGE_INTEGER nBeginTime;
    LARGE_INTEGER nEndTime;
    QueryPerformanceFrequency(&nFreq);
    QueryPerformanceCounter(&nBeginTime);//开始计时
        //...测试代码
        BubbleSort(R,n);
    QueryPerformanceCounter(&nEndTime);//停止计时
    time=(double)(nEndTime.QuadPart-nBeginTime.QuadPart)/(double)nFreq.QuadPart;//计算程序执行时间单位为s
    //cout<<"运行时间:"<<time*1000<<"ms"<<endl;
    printf("冒泡排序spend time: %f ms\n",(time *1000));
}

//折半插入排序时间算法分析 
void timeBinInsertSort(RecType R[],int n)
{
	double time=0;
    double counts=0;
    LARGE_INTEGER nFreq;
    LARGE_INTEGER nBeginTime;
    LARGE_INTEGER nEndTime;
    QueryPerformanceFrequency(&nFreq);
    QueryPerformanceCounter(&nBeginTime);//开始计时
        //...测试代码
        BinInsertSort(R,n);
    QueryPerformanceCounter(&nEndTime);//停止计时
    time=(double)(nEndTime.QuadPart-nBeginTime.QuadPart)/(double)nFreq.QuadPart;//计算程序执行时间单位为s
    //cout<<"运行时间:"<<time*1000<<"ms"<<endl;
    printf("折半插入排序spend time: %f ms\n",(time *1000));
}
//快速排序 
void timepartition(RecType R[],int s,int n)
{
	double time=0;
    double counts=0;
    LARGE_INTEGER nFreq;
    LARGE_INTEGER nBeginTime;
    LARGE_INTEGER nEndTime;
    QueryPerformanceFrequency(&nFreq);
    QueryPerformanceCounter(&nBeginTime);//开始计时
        //...测试代码
        partition(R,s,n);
    QueryPerformanceCounter(&nEndTime);//停止计时
    time=(double)(nEndTime.QuadPart-nBeginTime.QuadPart)/(double)nFreq.QuadPart;//计算程序执行时间单位为s
    //cout<<"运行时间:"<<time*1000<<"ms"<<endl;
    printf("快速排序spend time: %f ms\n",(time *1000));
}
//希尔排序算法 
void timeShellSort(RecType R[],int n)
{
	double time=0;
    double counts=0;
    LARGE_INTEGER nFreq;
    LARGE_INTEGER nBeginTime;
    LARGE_INTEGER nEndTime;
    QueryPerformanceFrequency(&nFreq);
    QueryPerformanceCounter(&nBeginTime);//开始计时
        //...测试代码
        ShellSort(R,n);
    QueryPerformanceCounter(&nEndTime);//停止计时
    time=(double)(nEndTime.QuadPart-nBeginTime.QuadPart)/(double)nFreq.QuadPart;//计算程序执行时间单位为s
    //cout<<"运行时间:"<<time*1000<<"ms"<<endl;
    printf("希尔排序spend time: %f ms\n",(time *1000));
}
//简单选择排序
void timeSelectSort(RecType R[],int n)
{
	double time=0;
    double counts=0;
    LARGE_INTEGER nFreq;
    LARGE_INTEGER nBeginTime;
    LARGE_INTEGER nEndTime;
    QueryPerformanceFrequency(&nFreq);
    QueryPerformanceCounter(&nBeginTime);//开始计时
        //...测试代码
        SelectSort(R,n);
    QueryPerformanceCounter(&nEndTime);//停止计时
    time=(double)(nEndTime.QuadPart-nBeginTime.QuadPart)/(double)nFreq.QuadPart;//计算程序执行时间单位为s
    //cout<<"运行时间:"<<time*1000<<"ms"<<endl;
    printf("简单选择排序spend time: %f ms\n",(time *1000));
} 
int main()
{
	system("color 00a");
	int n; 
	//RecType R[100]={437,240,896,189,916,662,475,793,722,153,810,230,754,967,727,51,254,642,362,815,602,862,216,995,865,883,342,938,166,324,20,184,582,705,901 367 852 795 317 591 116 750 164 974 220 36 419 646 448 415 4 23 310 694 143 293 505 740 74 531 673 982 577 215 547 244 471 512 893 497 715 238 733 838 30 393 650 564 963 596 859 17 344 51 591 172 421 763 493 196 91 657 661 959 734 981 294 703 166 136};
	int s;
	cout<<"请输入你要随机生成数字的个数"<<endl;
	cin>>n;
	RecType R[n];
	//int p[100];
	//int R[100];
	srand((unsigned)time(NULL));
	for(n=0;n<1000;n++)
{
	R[n].key=rand()%1000; //这个就可以产生0-999的随机数	//
}
	直接插入排序时间分析函数 
 	timeinsert(R,n);
 	//冒泡排序算法时间函数
 	timeBubbleSort(R,n);
 	//折半插入排序时间函数 
	timeBinInsertSort(R,n); 
	//快速排序算法
	s=R[0].key;
	timepartition(R,s,n);
	//希尔排序 
	timeShellSort(R,n); 
	//简单选择排序算法
	SelectSort(R,n);
}


 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值