# 泛型插入排序

#pragma once

#include <iterator>

#include <functional>

template<

class bidirectional_iterator,

template<class> class greater_compare = std::greater

>

class insertion_sort_functor

{

public:

bool operator()(bidirectional_iterator first, bidirectional_iterator last)const

{

typedef bidirectional_iterator::value_type value_type;

typedef greater_compare<value_type> gc;

gc gc_comparer;

bidirectional_iterator j = next_iterator(first);

for( ; j!=last ; ++j)

{

value_type key = *j;

bidirectional_iterator i = last_iterator(j);

while(std::distance(first,i)>0 && gc_comparer(*i,key) )

{

*next_iterator(i) = *i;

--i;

}

if(i == first && gc_comparer(*i,key))

{

*next_iterator(i) = *i;

*i = key;

continue;

}

*next_iterator(i)= key;

}

return true;

}

private:

bidirectional_iterator last_iterator(const bidirectional_iterator& i)const

{

bidirectional_iterator j(i);

return --j;

}

bidirectional_iterator next_iterator(const bidirectional_iterator& i)const

{

bidirectional_iterator j(i);

return ++j;

}

};

int a[]={345,67,56,6,4567467,65768,7686,-34635,67,57678,-235};

vector<int> vi(a,a+sizeof(a)/sizeof(int));

list<int> li(a,a+sizeof(a)/sizeof(int));

insertion_sort(a ,sizeof(a)/sizeof(int));

insertion_sort_functor<vector<int>::iterator, std::less>()

(vi.begin(), vi.end());

insertion_sort_functor<list<int>::iterator, std::greater>()

(li.begin(), li.end());

#### Java中泛型选择排序（升序），继承Comparable接口

2014-12-19 13:14:48

#### Java泛型实现各个排序

2014-04-26 16:05:11

#### 介绍4个常用的排序法。冒泡排序、插入排序、快速排序、选择排序法

2012-03-30 20:11:14

#### 排序算法——插入排序的图解、代码实现以及时间复杂度分析

2018-01-21 15:47:25

#### php四种基础算法：冒泡，选择，插入和快速排序法

2016-10-10 16:16:06

#### 四种简单的排序算法 -----笔试常考非常有用

2013-07-10 18:31:11

#### （数据结构排序的实验四）快速，冒泡，简单选择，直接插入排序的c语言实现！！

2014-12-11 19:05:42

#### 《数据结构 》排序试题附答案

2013-06-17 22:21:07

#### 泛型详解21

2015-05-11 13:25:03

#### 算法导论7.4-5

2014-10-06 17:14:48