📄 insertsort.h
字号:
#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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -