?? sort.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 + -