工厂模式使用排序算法(c++实现)

一.设计抽象排序算法基类 class AbstractSort{};

1.析构函数~AbstractSort()和排序算法sort() 设置为虚函数

2.基类公共方法print(),作用为打印数组

3.基类公共方法setNums()  设置基类私有成员nums的值

4.基类公共方法getNums()  获取基类私有成员nums的值

//抽象排序算法
class AbstractSort {
private:
	vector<int> nums;
public:
	AbstractSort(vector<int> _nums){

		this->nums = _nums;
	}
	AbstractSort() {};
	virtual ~AbstractSort() {};
	virtual void sort()=0;

	void print() {
		int n= nums.size();
		for (int i = 0; i < n; i++) {
			cout << nums[i] << " ";
		}
		cout << endl;
	}
	void setNums(vector<int> nums) {
		this->nums = nums;
	}
	vector<int> getNums() {
		return this->nums;
	}
};

二、设计派生类排序算法

注意:

(1)派生类不能直接访问基类的私有成员,派生类必须使用基类的公有方法来访问私有的基类成员;

(2)创建派生类对象时,会将派生类构造函数的参数传递给基类的构造函数,即构造派生类对象时,先实例化基类对象,再创建派生类;

(3)当基类的构造函数带参数,那么派生类的构造函数必须显式地写构造函数,给基类的构造函数的参数赋值;

(4)基类的构造函数不带参数时派生类的构造函数可以不由程序员写出来,有系统默认提供。

1.冒泡排序

//冒泡排序 继承自抽象排序
class BubbleSort :public AbstractSort{
public:
	BubbleSort(vector<int> _nums):AbstractSort(_nums){};
	~BubbleSort() {};
	void sort(){
		vector<int> nums=AbstractSort::getNums();
		bubbleSort(nums);
		AbstractSort::setNums(nums);
	}
	void bubbleSort(vector<int> &nums) {
		int n = nums.size();
		for (int i = 0; i < n; i++) {
			bool isSort = true;

			for (int j = 0; j < n - i - 1; j++) {
				if (nums[j] > nums[j + 1]) {
					swap(nums[j], nums[j + 1]);
					isSort = false;
				}
			}
			if (isSort) break;
		}
	}
};

2.快排

//快排 继承抽象排序
class QuickSort :public AbstractSort {
public:
	QuickSort(vector<int> _nums) :AbstractSort(_nums) {}
	~QuickSort() {}
	void sort() {
		vector<int> nums = AbstractSort::getNums();
		int n = nums.size();
		quickSort(nums,n,0,n-1);
		AbstractSort::setNums(nums);
	}
	void quickSort(vector<int> &nums,int n,int begin,int end) {
		if (begin >= 0 and begin < n and end >= 0 and end < n and begin < end) {
			int left = begin;
			int right = end;
			int vot = nums[left];
			while (left != right) {
				while (left < right and vot <= nums[right]) right--;
				if (left < right) nums[left++] = nums[right];
				while (left < right and vot >= nums[left]) left++;
				if (left < right) nums[right--] = nums[left];
			}
			nums[left] = vot;
			quickSort(nums, n, begin, right - 1);
			quickSort(nums, n, left+1, end);
		}
		else
			return;
	}
};

3.插入排序

//插入排序 继承自抽象排序
class InsertSort :public AbstractSort {
private:
	
public:
	InsertSort(vector<int> _nums) :AbstractSort(_nums) {}
	~InsertSort() {}

	void sort() {

		vector<int> nums = AbstractSort::getNums();
		insertSort(nums);
		AbstractSort::setNums(nums);
	}
	void insertSort(vector<int> &nums) {
		int n = nums.size();
		for (int i = 0; i < n; i++) {
			int val = nums[i];
			int j = 0;
			for (j = i - 1; j >= 0 and nums[j] > val; j--) {
				nums[j + 1] = nums[j];	
			}
			nums[j + 1] = val;
		}
	}
};

4.希尔排序

//希尔排序 继承自抽象排序
class ShellSort :public AbstractSort {
private:
	
public:
	ShellSort(vector<int> _nums) :AbstractSort(_nums) {}
	~ShellSort() {}
	void sort() {
		vector<int> nums = AbstractSort::getNums();
		shellSort(nums);
		AbstractSort::setNums(nums);
	}
	void shellSort(vector<int> &nums) {
		int n = nums.size();
		int gap = 1;
		while (gap < n) gap = gap * 3 + 1;
		while (gap > 0) {
			for (int i = gap; i < n; i++) {
				int val = nums[i];
				int j = i - gap;
				for (; j >= 0 and nums[j]>val; j = j - gap) {
					nums[j + gap] = nums[j];
				}
				nums[j + gap] = val;
			}
			gap /= 3;
		}
	}
};

5.选择排序

//选择排序 继承自抽象排序
class SelectionSort :public AbstractSort {

public:
	SelectionSort(vector<int> _nums) :AbstractSort(_nums) {};
	~SelectionSort() {};

	void sort() {
		vector<int> nums = AbstractSort::getNums();
		selectionSort(nums);
		AbstractSort::setNums(nums);
	}
	void selectionSort(vector<int> &nums) {
		int n = nums.size();
		for (int i = 0; i < n; i++) {
			int min = i;
			for (int j = i+1; j<n; j++) {
				if (nums[j] < nums[min]) min = j;
			}
			swap(nums[i],nums[min]);
		}
	}
};

6.堆排序 大根堆和小根堆的实现都有

//堆排序 继承自抽象排序
class HeapSort :public AbstractSort {
private:
	bool isAsc;
public:
	HeapSort(vector<int> _nums) :AbstractSort(_nums) {
		this->isAsc = true; //默认升序输出
	};
	~HeapSort() {};

	void sort() {

		vector<int> nums = AbstractSort::getNums();
		heapSort(nums);
		AbstractSort::setNums(nums);
	}
	void heapSort(vector<int> &nums) {
		int n = nums.size();
		buildHeap(nums,n);
		for (int i = n-1; i>=0; i--) {
			swap(nums[i], nums[0]);
			n--;
			HeapAdjust(nums,0, n);
		}
	}
	//建立堆  建立大根堆还是小根堆取决于选择升序输出还是降序输出
	void buildHeap(vector<int> &nums, int n) {  
		for (int i = n / 2; i >= 0; i--) {
				HeapAdjust(nums, i, n);
		}
	}
	//堆调整
	void HeapAdjust(vector<int> &nums, int index, int n) {
		int leftChild = 2 * index + 1;
		int righChild = 2 * index + 2;
		int present = index;
		if (isAsc) { //选择升序输出 建立大根堆
			if (leftChild < n and nums[leftChild] > nums[present])
				present = leftChild;
			if (righChild < n and nums[righChild] > nums[present])
				present = righChild;
		}
		else {       //选择降序输出 建立小根堆
			if (leftChild < n and nums[leftChild] < nums[present])
				present = leftChild;
			if (righChild < n and nums[righChild] < nums[present])
				present = righChild;
		}
		if (present != index) {
			swap(nums[present], nums[index]);
			HeapAdjust(nums, present, n);
		}
	}
};

7.归并排序

//归并排序 继承自抽象排序
class MergeSort :public AbstractSort {
public:
	MergeSort(vector<int> _nums) :AbstractSort(_nums) {
		
	};
	~MergeSort() {};

	void sort() {

		vector<int> nums = AbstractSort::getNums();
		int n = nums.size();
		vector<int> numsTemp(n);
		mergeSort(nums,numsTemp,0,n-1);
		AbstractSort::setNums(nums);
	}
	void mergeSort(vector<int> &nums, vector<int> &numsTemp,int begin,int end) 
	{
		if (begin >= end) return;
		int mid = begin + (end - begin) / 2;
		mergeSort(nums, numsTemp, begin, mid);
		mergeSort(nums, numsTemp, mid + 1, end);
		merge(nums, numsTemp, begin, mid, end);
	}
	void merge(vector<int> &nums, vector<int> &numsTemp, int begin, int mid, int end) {
		//复制要合并的数据
		for (int i = begin; i <= end; i++) numsTemp[i] = nums[i];
		int left = begin;		//左边首位下标
		int right = mid + 1;	//右边首位下标
		for (int k = begin; k <= end; k++) {
			if (left > mid) 
				nums[k] = numsTemp[right++];
			else if (right > end) 
				nums[k] = numsTemp[left++];
			else if (numsTemp[left] >  numsTemp[right]) 
				nums[k] = numsTemp[right++];
			else if (numsTemp[left] <= numsTemp[right])
				nums[k] = numsTemp[left++];
		}
	}
};

对排序算法直接调用测试

//直接测试排序算法
void test(vector<int> nums) {
	AbstractSort *sort = NULL;

	cout << "Bubble Sort:" << endl;
	sort = new BubbleSort(nums);
	sort->print();
	sort->sort();
	sort->print();
	cout << endl;
	delete sort;

	cout << "Quick Sort:" << endl;
	sort = new QuickSort(nums);
	sort->print();
	sort->sort();
	sort->print();
	cout << endl;
	delete sort;

	cout << "Insert Sort:" << endl;
	sort = new InsertSort(nums);
	sort->print();
	sort->sort();
	sort->print();
	cout << endl;
	delete sort;

	cout << "Shell Sort:" << endl;
	sort = new ShellSort(nums);
	sort->print();
	sort->sort();
	sort->print();
	cout << endl;
	delete sort;

	cout << "Selection Sort:" << endl;
	sort = new SelectionSort(nums);
	sort->print();
	sort->sort();
	sort->print();
	cout << endl;
	delete sort;

	cout << "Heap Sort:" << endl;
	sort = new HeapSort(nums);
	sort->print();
	sort->sort();
	sort->print();
	cout << endl;
	delete sort;

	cout << "Merge Sort:" << endl;
	sort = new MergeSort(nums);
	sort->print();
	sort->sort();
	sort->print();
	cout << endl;
	delete sort;
}

三、简单工厂封装和调用排序算法

//简单工厂
class SortSimpleFactory {
public:
	static AbstractSort *CreateSortMethod(string flag,vector<int> nums) {
		if (flag == "BubbleSort") {
			return new BubbleSort(nums);
		}
		else if (flag == "QuickSort") {
			return new QuickSort(nums);
		}
		else if (flag == "InsertSort") {
			return new InsertSort(nums);
		}
		else if (flag == "ShellSort") {
			return new ShellSort(nums);
		}
		else if (flag == "SelectionSort") {
			return new SelectionSort(nums);
		}
		else if (flag == "HeapSort") {
			return new HeapSort(nums);
		}
		else if (flag == "MergeSort") {
			return new MergeSort(nums);
		}
		else {
			return NULL;
		}
	}
};
//简单工厂测试
void testSortSimpleFactory(vector<int> nums) {
	SortSimpleFactory *factory = new  SortSimpleFactory;
	AbstractSort *sort = NULL;
	
	vector<string> useSort = { "BubbleSort","QuickSort" ,"InsertSort" ,"ShellSort" ,"SelectionSort","HeapSort","MergeSort" };
	for (string s : useSort) {
		cout<<s<< ":" << endl;
		sort = factory->CreateSortMethod(s, nums);
		sort->print();
		sort->sort();
		sort->print();
		cout << endl;
		delete sort;
	}
	delete factory;
}

四、抽象工厂封装和调用排序算法

//抽象工厂
class SortAbstractFactory {
public:
	virtual  AbstractSort* CreateSortMethod(vector<int> nums) = 0;
};
//冒泡工厂
class BubbleSortFactory:public SortAbstractFactory
{
public:
	virtual AbstractSort* CreateSortMethod(vector<int> nums)
	{
		return new BubbleSort(nums);
	}
};
//快排工厂
class QuickSortFactory :public SortAbstractFactory
{
public:
	virtual AbstractSort* CreateSortMethod(vector<int> nums)
	{
		return new QuickSort(nums);
	}
};
//插入排序工厂
class InsertSortFactory :public SortAbstractFactory
{
public:
	virtual AbstractSort* CreateSortMethod(vector<int> nums)
	{
		return new InsertSort(nums);
	}
};
//希尔排序工厂
class ShellSortFactory :public SortAbstractFactory
{
public:
	virtual AbstractSort* CreateSortMethod(vector<int> nums)
	{
		return new ShellSort(nums);
	}
};
//选择排序工厂
class SelectionSortFactory :public SortAbstractFactory
{
public:
	virtual AbstractSort* CreateSortMethod(vector<int> nums)
	{
		return new SelectionSort(nums);
	}
};
//堆排序工厂
class HeapSortFactory :public SortAbstractFactory
{
public:
	virtual AbstractSort* CreateSortMethod(vector<int> nums)
	{
		return new  HeapSort(nums);
	}
};
//归并排序工厂
class MergeSortFactory :public SortAbstractFactory
{
public:
	virtual AbstractSort* CreateSortMethod(vector<int> nums)
	{
		return new MergeSort(nums);
	}
};

void testSortAbstractFactory(vector<int> nums) {

	SortAbstractFactory* factory = NULL;
	AbstractSort *sort = NULL;

	factory = new BubbleSortFactory;
	cout << "Bubble Sort:" << endl;
	sort = factory->CreateSortMethod(nums);
	sort->print();
	sort->sort();
	sort->print();
	cout << endl;
	delete sort;
	delete factory;
	
	factory = new QuickSortFactory;
	sort = factory->CreateSortMethod(nums);
	cout << "Quick Sort:" << endl;
	sort->print();
	sort->sort();
	sort->print();
	cout << endl;
	delete sort;
	delete factory;

	factory = new InsertSortFactory;
	sort = factory->CreateSortMethod(nums);
	cout << "Insert Sort:" << endl;
	sort->print();
	sort->sort();
	sort->print();
	cout << endl;
	delete sort;
	delete factory;

	factory = new ShellSortFactory;
	sort = factory->CreateSortMethod(nums);
	cout << "Shell Sort:" << endl;
	sort->print();
	sort->sort();
	sort->print();
	cout << endl;
	delete sort;
	delete factory;

	factory = new SelectionSortFactory;
	sort = factory->CreateSortMethod(nums);
	cout << " Selection Sort:" << endl;
	sort->print();
	sort->sort();
	sort->print();
	cout << endl;
	delete sort;
	delete factory;

	factory = new HeapSortFactory;
	sort = factory->CreateSortMethod(nums);
	cout << "Heap Sort:" << endl;
	sort->print();
	sort->sort();
	sort->print();
	cout << endl;
	delete sort;
	delete factory;

	factory = new MergeSortFactory;
	sort = factory->CreateSortMethod(nums);
	cout << "Merge Sort:" << endl;
	sort->print();
	sort->sort();
	sort->print();
	cout << endl;
	delete sort;
	delete factory;
}

五、测试样例和测试结果

#include<iostream>
#include<vector>
#include<string>
using namespace std;

int main() 
{
	cout << "input the length of nums: ";
	int n;
	cin >> n;
	cout << endl;
	vector<int> nums;
	//随机数放入nums中
	for (int i = 0; i < n; i++) {
		nums.push_back(rand()%n);
	}
	//test(nums);						//直接对各个排序算法进行测试
	//testSortSimpleFactory(nums);		//简单工厂模式测试
	testSortAbstractFactory(nums);	    //工厂模式测试
	
	system("pause");
	return 0;
}

测试结果

参考:排序算法https://www.cnblogs.com/fengyumeng/p/10994279.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值