⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.cpp

📁 对8组包含不同个数据的向量进行排序
💻 CPP
字号:
#include<stdlib.h>
#include<assert.h>
#include"Vector.h"
#include"Iterator.h"
#include"VectorIterator.h"
#include"orderdVector.h"
#include"orderedVectorIterator.h"
int cf,mf,bcf,bmf;

template <class T>void swap(vector <T>&vec,int i,int j)
{  
	T temp=vec[i];
	vec[i]=vec[j];
	vec[j]=temp;
}
template <class T>void bubbleSort(vector<T>&vec)
{   cf=0;
    mf=0;
	int top,i;
	for (top=vec.length()-1;top>0;top--)
	{
		for(i=0;i<top;i++)
		{   cf++;
			if(vec[i+1]<vec[i])
			{
				swap(vec,i+1,i);
				mf=mf+3;
			}
		}
	}
}
template <class T>void insertionSort(vector<T>&vec)
{  
	cf=0;
    mf=0;
	int n=vec.length(),j,next;
	T temp;
	for (next =1;next<n;next++)
	{
		temp=vec[next];
		for(j=next-1;j>=0&&temp<vec[j];j--)		    
		{
			vec[j+1]=vec[j];
			cf++;
			mf++;
		}
		 vec[j+1]=temp;
		 mf++;
	}
}
template<class T>void selectionSort(vector<T>&vec)
{
	cf=0;
    mf=0;
	int largest,top,j;
	for(top=vec.length()-1;top>0;top--)
	{
		for(largest=0, j=1;j<=top;j++)
		{
			if(vec[largest]<vec[j])
			{
				largest=j;
				cf++;
			}
		}
		if(top!=largest)
		{
			swap(vec,top,largest);
			mf=mf+3;
		}
	}
}
template<class T>
int partition(vector<T>&v,int low,int high)
{    
	bcf=0;
    bmf=0;
	T pivot=v[low];
	while(low<high){
		while(low<high&&v[high]>=pivot)
		{
			bcf=bcf+2;
			high--;
		}
		if(low<high){
			bcf++;
			v[low]=v[high];
			bmf++;
			low++;
		}
		while(low<high&&v[low]<=pivot)
		{
			bcf=bcf+2;
			low++;
		}
		if (low<high){
			bcf++;
			v[high]=v[low];
			bmf++;
			high--;
	}
}
v[high]=pivot;
bmf++;
return high;
}
template<class T>void quickSort(vector<T>&v,int low,int high)
{   
	if (low>=high)
	{
		cf++;
		mf=mf;
		return;
	}
		cf++;
		mf=mf;
	int mIndex=partition(v,low,high);
	cf=cf+bcf;
	mf=mf+bmf;
	quickSort(v,low,mIndex-1);
	quickSort(v,mIndex+1,high);
}
    template<class T>void quickSort(vector<T>&v)
	{
		quickSort(v,0,v.length()-1);
	}
void main()
{   int kf=1,hfh=1;
	int geshu=0;
	int xff;
	char ckf,hkf;
	cout<<"你好,这里是插入、起泡、选择、快速排序等算法的执行效率比较。"<<endl;
	while(kf==1){
	do{

	cout<<"请确认所要排序的随机数的个数:";
	cin>>geshu;
	}while (geshu<=0);
	srand((unsigned)geshu);
	vector<int> fv(geshu);
	for(int i=0;i<geshu;i++)
		fv[i]=rand();
    cout<<"排序前的向量为:"<<endl;
	for(unsigned j=0;j<fv.length();j++)
	{	if(j==0)cout<<"("<<j+1<<")"<<fv[j]<<"  ";	
	else if((j%6)==0)cout<<"("<<j+1<<")"<<fv[j]<<endl;
	 else cout<<"("<<j+1<<")"<<fv[j]<<"  ";

	}
	cout<<endl;
	hfh=1;
	while(hfh==1){

	do{
	cout<<"请选择排序方法:"<<endl;
	cout<<"1------------------------------插入排序"<<endl;
	cout<<"2------------------------------起泡排序"<<endl;
	cout<<"3------------------------------选择排序"<<endl;
	cout<<"4------------------------------快速排序"<<endl;
	cin>>xff;
	}while (!(xff==1||xff==2||xff==3||xff==4));
	vector<int> cfv((unsigned)geshu);
	cfv=fv;
	if(xff==1){
		insertionSort(cfv);
		cout<<"插入排序后的结果:"<<endl;
	for(unsigned j=0;j<cfv.length();j++)
	{	if(j==0)cout<<"("<<j+1<<")"<<cfv[j]<<"  ";	
	else if(((j%6)==0)||(j==cfv.length()-1))cout<<"("<<j+1<<")"<<cfv[j]<<endl;
	 else cout<<"("<<j+1<<")"<<cfv[j]<<"  ";

	}
		cout<<"插入排序的比较次数为:"<<cf<<endl;
		cout<<"插入排序的移动次数为:"<<mf<<endl;
	}
	else if(xff==2)
	{   
		cfv=fv;
		bubbleSort(cfv);
				cout<<"起泡排序后的结果:"<<endl;
	for(unsigned j=0;j<cfv.length();j++)
	{	if(j==0)cout<<"("<<j+1<<")"<<cfv[j]<<"  ";	
	else if(((j%6)==0)||(j==cfv.length()-1))cout<<"("<<j+1<<")"<<cfv[j]<<endl;
	 else cout<<"("<<j+1<<")"<<cfv[j]<<"  ";

	}

		cout<<"起泡排序的比较次数为:"<<cf<<endl;
		cout<<"起泡排序的移动次数为:"<<mf<<endl;
	}
	else if(xff==3)
	{   
		cfv=fv;
		 selectionSort(cfv);
				cout<<"选择排序后的结果:"<<endl;
	for(unsigned j=0;j<cfv.length();j++)
	{	if(j==0)cout<<"("<<j+1<<")"<<cfv[j]<<"  ";	
	else if(((j%6)==0)||(j==cfv.length()-1))cout<<"("<<j+1<<")"<<cfv[j]<<endl;
	 else cout<<"("<<j+1<<")"<<cfv[j]<<"  ";

	}
	
		cout<<"选择排序的比较次数为:"<<cf<<endl;
		cout<<"选择排序的移动次数为:"<<mf<<endl;
	}
	else if(xff==4)
	{
       	cfv=fv;
		 quickSort(cfv);
		
		cout<<"快速排序后的结果:"<<endl;
	for(unsigned j=0;j<cfv.length();j++)
	{	if(j==0)cout<<"("<<j+1<<")"<<cfv[j]<<"  ";	
	else if(((j%6)==0)||(j==cfv.length()-1))cout<<"("<<j+1<<")"<<cfv[j]<<endl;
	 else cout<<"("<<j+1<<")"<<cfv[j]<<"  ";

	}
		cout<<"快速排序的比较次数为:"<<cf<<endl;
		cout<<"快速排序的移动次数为:"<<mf<<endl;
		 cf=mf=0;
	}
       cout<<"你用其他的排序方法吗?(Y/N)?";
	   cin>>hkf;
     if(hkf=='y'||hkf=='Y')hfh=1;
      else hfh=0;

	}
cout<<"你要再来新的数据比较?(Y/N)?";
cin>>ckf;
if(ckf=='y'||ckf=='Y')kf=1;
else kf=0;
}
}



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -