insertsort.h

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

H
47
字号
#ifndef INSERTSORT_H
#define INSERTSORT_H
#include<iostream.h>

template<class Elem>
class InsertSort:public sort<Elem>
{
	public:
		InsertSort(){compareNum = 0 ; moveNum = 0 ;};
		virtual void Sorts(Elem Array[], int n);////
		void print_i(Elem Array[], int n);
		int passCompare(){return compareNum;};
		int passMove(){return moveNum;};
	private:
		int compareNum;/////比较次数
		int moveNum;/////移动次数
};
template<class Elem>
void InsertSort<Elem>::Sorts(Elem Array[], int n)
{
	Elem  temp;
	for (int i = 1; i < n ; i++)
	{
		temp=Array[i];
		int j = i - 1;///////int j = n-1;
		while( (j >= 0) && (compare(temp,Array[j]) == -1) )
		{
			Array[j+1]=Array[j];
			j--;
			compareNum++;
			moveNum++;
		}
		compareNum++;
		Array[j+1]=temp;
		moveNum++;
	}
}
template<class Elem>
void InsertSort<Elem>::print_i(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 + -
显示快捷键?