主题:[原创]各们大佬,帮帮忙看一下哪里有问题
下面是一个插入排序的代码,很有问题,希望各位大虾指点指点,在此先谢过了!
class BasicComp
{
public:
template<class Elem>
static bool lt(Elem x, Elem y){return x < y;}
template<class Elem>
static bool eq(Elem x, Elem y){return x == y;}
template<class Elem>
static bool gt(Elem x, Elem y){return x > y;}
};
template<class Elem>
void swap(Elem* h, int i, int j)
{
Elem temp;
temp=h[i];
h[i]=h[j];
h[j]=temp;
}
template<class Elem,class Comp>
void inssort(Elem A[],int n){
for(int i=1;i<n;i++)
for(int j=i;(j>0)&&(Comp::lt(A[j],A[j-1]));j--)
swap(A,j,j-1);
}
#include<iostream.h>
void main()
{
int B[]={42,20,17,13,28,14,23,15};
for(int i=0;i<8;i++)
cout<<B[i]<<" ";
cout<<endl;
inssort(B,8);
for(int j=0;j<8;j++)
cout<<B[j]<<" ";
cout<<endl;
}
class BasicComp
{
public:
template<class Elem>
static bool lt(Elem x, Elem y){return x < y;}
template<class Elem>
static bool eq(Elem x, Elem y){return x == y;}
template<class Elem>
static bool gt(Elem x, Elem y){return x > y;}
};
template<class Elem>
void swap(Elem* h, int i, int j)
{
Elem temp;
temp=h[i];
h[i]=h[j];
h[j]=temp;
}
template<class Elem,class Comp>
void inssort(Elem A[],int n){
for(int i=1;i<n;i++)
for(int j=i;(j>0)&&(Comp::lt(A[j],A[j-1]));j--)
swap(A,j,j-1);
}
#include<iostream.h>
void main()
{
int B[]={42,20,17,13,28,14,23,15};
for(int i=0;i<8;i++)
cout<<B[i]<<" ";
cout<<endl;
inssort(B,8);
for(int j=0;j<8;j++)
cout<<B[j]<<" ";
cout<<endl;
}