bubblesort.h

来自「各种排序算法BubbleSort、DichotomySort、HeapSort、」· C头文件 代码 · 共 46 行

H
46
字号
#ifndef BUBBLESORT_H
#define BUBBLESORT_H
#include<iostream.h>

template<class Elem>
class BubbleSort:public sort<Elem>
{
	public:
		BubbleSort(){compareNum = 0 ; moveNum = 0;};
		virtual void Sorts(Elem Array[], int n);////
		void print_b(Elem Array[], int n);
	private:
		int compareNum;/////比较次数
		int moveNum;/////移动次数
};
template<class Elem>
void BubbleSort<Elem>::Sorts(Elem Array[], int n)
{
	bool noSwap;
	for(int i = 0 ; i < n ; i++)
	{
		noSwap = true;
		for(int j = n-1 ; j >= i ; j--)
		{
			compareNum++;
			if(compare(Array[j] , Array[j-1]) == -1)
			{
				swap(Array , j , j-1);
				moveNum++;
				noSwap = false;
			}
		}
		if(noSwap)
			return;
	}
}
template<class Elem>
void BubbleSort<Elem>::print_b(Elem Array[], int n)
{
	cout<<"        冒泡排序法         "<<endl;
	cout<<"+++++++++++++++++++++++++++"<<endl;
	cout<<"比较次数:"<<compareNum<<endl;
	cout<<"移动次数:"<<moveNum<<endl;
	//print(Array , n);
}
#endif

⌨️ 快捷键说明

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