selectsort.h

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

H
43
字号
#ifndef SELECTSORT_H
#define SELECTSORT_H
#include<iostream.h>

template<class Elem>
class SelectSort:public sort<Elem>
{
	public:
		SelectSort(){compareNum = 0 ; moveNum = 0;};
		virtual void Sorts(Elem Array[], int n);////
		void print_s(Elem Array[], int n);
	private:
		int compareNum;/////比较次数
		int moveNum;/////移动次数
};
template<class Elem>
void SelectSort<Elem>::Sorts(Elem Array[], int n)
{
	for(int i = 0 ; i < n-1 ; i++)///for(int i = 0 ; i < n ; i++)选择剩余记录里最小的一个
	{
		int Smallest = i;
		for(int j = i + 1 ; j < n ; j++)
		{	
			if (compare(Array[j], Array[Smallest]) == -1)
			{
				Smallest = j;
			}
			compareNum++;
		}
		swap(Array,i,Smallest);
		moveNum++;
	}
}
template<class Elem>
void SelectSort<Elem>::print_s(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 + -
显示快捷键?