level 11
gameloftyou
楼主
#include <iostream>
using namespace std;
template <class T> struct traits {
typedef typename T::value_type value_type;
};
template <class T>
struct traits<T *>
{
typedef T value_type;
};
template <class T>
void InsertSort1(const T &begin,const T &end)
{
for(auto it=begin+1;it!=end;++it)
{
typename traits<T>::value_type value = *it;
auto jt = it-1;
while(value<*jt&&jt!=begin-1)
{
*(jt+1) = *jt;
--jt;
}
*(jt+1) = value;
}
}
template <class T>
void InsertSort2(const T &begin,const T &end)
{
for(auto it=begin+1;it!=end;++it)
{
decltype(*it) value = *it;
auto jt = it-1;
while(value<*jt&&jt!=begin-1)
{
*(jt+1) = *jt;
--jt;
}
*(jt+1) = value;
}
}
int main()
{
int a[6]{ 5, 3, 6, -2, 8, 9 };
int b[6]{ 5, 3, 6, -2, 8, 9 };
InsertSort1(a+0,a+6);
InsertSort2(b+0,b+6);
for(auto i:a) cout<<i<<",";
cout<<endl;
for(auto i:b) cout<<i<<",";
return 0;
}
2013年11月15日 07点11分
1
using namespace std;
template <class T> struct traits {
typedef typename T::value_type value_type;
};
template <class T>
struct traits<T *>
{
typedef T value_type;
};
template <class T>
void InsertSort1(const T &begin,const T &end)
{
for(auto it=begin+1;it!=end;++it)
{
typename traits<T>::value_type value = *it;
auto jt = it-1;
while(value<*jt&&jt!=begin-1)
{
*(jt+1) = *jt;
--jt;
}
*(jt+1) = value;
}
}
template <class T>
void InsertSort2(const T &begin,const T &end)
{
for(auto it=begin+1;it!=end;++it)
{
decltype(*it) value = *it;
auto jt = it-1;
while(value<*jt&&jt!=begin-1)
{
*(jt+1) = *jt;
--jt;
}
*(jt+1) = value;
}
}
int main()
{
int a[6]{ 5, 3, 6, -2, 8, 9 };
int b[6]{ 5, 3, 6, -2, 8, 9 };
InsertSort1(a+0,a+6);
InsertSort2(b+0,b+6);
for(auto i:a) cout<<i<<",";
cout<<endl;
for(auto i:b) cout<<i<<",";
return 0;
}