回 帖 发 新 帖 刷新版面

主题:[原创]各们大佬,帮帮忙看一下哪里有问题

下面是一个插入排序的代码,很有问题,希望各位大虾指点指点,在此先谢过了!

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;
}

回复列表 (共1个回复)

沙发

你这哪是插入排序啊,明明是交换排序嘛

我来回复

您尚未登录,请登录后再回复。点此登录或注册