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

📄 sort.cpp

📁 我自己写的vc数据结构的作业
💻 CPP
字号:
/*************************************
 * 本例采用了设计模式中的strategy模式
 * 将所有的排序算法封装到SortStratety类
 * 由SortAlgorithm选择算法,客户端需要
 * 提供具体的算法类,然后经行排序
 * *********************************/

#include <iostream>
#include <string>
#include "sortStrategy.h"
#include "sortAlgorithm.h"

using namespace std;

int main()
{
	int length=5;

	SortStrategy<int>* ps;


	//----------------------------
	int a[]={123,45,55,0,6};
	cout<<"冒泡排序"<<endl;
	ps=new BubbleSort<int>;             //冒泡排序
	SortAlgorithm<int>* psa=new SortAlgorithm<int>(ps);
	psa->sort(a,5);
	
	for(int i=0;i<length;i++)
		cout<<a[i]<<' ';
	cout<<endl;

	//----------------------------
	int a1[]={123,45,55,0,6};
	cout<<"选择排序"<<endl;
	ps=new SelectSort<int>;             //选择排序
	psa=new SortAlgorithm<int>(ps);
	psa->sort(a1,5);
	
	for(int i=0;i<length;i++)
		cout<<a1[i]<<' ';
	cout<<endl;


	//----------------------------
	int a2[]={123,45,55,0,6};
	cout<<"插入排序"<<endl;
	ps=new InsertSort<int>;             //插入排序
	psa=new SortAlgorithm<int>(ps);
	psa->sort(a2,5);
	
	for(int i=0;i<length;i++)
		cout<<a2[i]<<' ';
	cout<<endl;


	//----------------------------
	int a3[]={123,45,55,0,6};
	cout<<"快速排序"<<endl;
	ps=new QuickSort<int>;             //快速排序
	psa=new SortAlgorithm<int>(ps);
	psa->sort(a3,5);
	
	for(int i=0;i<length;i++)
		cout<<a3[i]<<' ';
	cout<<endl;


	//----------------------------
	int a4[]={123,45,55,0,6};
	cout<<"Shell排序"<<endl;
	ps=new ShellSort<int>;             //Shell排序
	psa=new SortAlgorithm<int>(ps);
	psa->sort(a4,5);
	
	for(int i=0;i<length;i++)
		cout<<a[i]<<' ';
	cout<<endl;


	//----------------------------
	int a5[]={123,45,55,0,6};
	cout<<"堆排序"<<endl;
	ps=new HeapSort<int>;             //堆排序
	psa=new SortAlgorithm<int>(ps);
	psa->sort(a5,5);
	
	for(int i=0;i<length;i++)
		cout<<a[i]<<' ';
	cout<<endl;


	//----------------------------
	int a6[]={123,45,55,0,6};
	cout<<"归并排序"<<endl;
	ps=new MergeSort<int>;             //归并排序
	psa=new SortAlgorithm<int>(ps);
	psa->sort(a6,5);
	
	for(int i=0;i<length;i++)
		cout<<a[i]<<' ';
	cout<<endl;

	//代码结尾
	if(0!=psa)
		delete psa;
	if(0!=ps)
		delete ps;

	return 0;
}

⌨️ 快捷键说明

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