请按任意键继续...

  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  21 随笔 :: 0 文章 :: 2 评论 :: 0 Trackbacks
今晚用模板函数实现了插入排序算法
template<typename T>
void InsertSort(vector<T> & vecT)
{
   typename vector<T>::iterator it1, it2, it1Tmp;
    it1 = vecT.begin() + 1;
    while(it1 != vecT.begin())
    {
         T key = *it1;
          it1Tmp = it1;
          it2 = it1 - 1;
          while(it2 >= vecT.begin() && key < *it2)
           {
                  *it1Tmp = *it1;
                  it1--;
                  it2--;
           }
            *it1Tmp = key;
    }
}
posted on 2012-10-29 23:01 星哥 阅读(258) 评论(2)  编辑 收藏 引用

评论

# re: InsertSort 2013-07-15 17:09 李博
不过 单纯的因为排序的名字 而就写成这样是不行的
第一眼看到 while里面的while马上了解 他想在前面 也就是已经排序的元素中插入元素 我觉得还是 应该用二分替换第二个while !

qq348634371 希望认识更多喜欢代码的朋友 !  回复  更多评论
  

# re: InsertSort 2013-07-15 17:10 李博
闲来无事 就一篇篇看你的文章 没有留言勿怪   回复  更多评论
  


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理