高效的插入排序
来源:互联网 发布:免费网络推广平台 编辑:程序博客网 时间:2024/05/16 14:08
#include <algorithm>
using namespace std;
template<class RandomAccessIterator,class T>//插入排序的辅助函数1
inline void InnerInsert(RandomAccessIterator first,RandomAccessIterator last,T *);
template<class RandomAccessIterator,class T>//插入排序的辅助函数2
inline void UnguardedInnerInsert(RandomAccessIterator last,T value);
template<class RandomAccessIterator>
void InsertSort(RandomAccessIterator first,RandomAccessIterator last)
{
if(first == last)
return;
for(RandomAccessIterator iter = first + 1;iter != last;++iter)//外层循环
{
InnerInsert(first,iter);//内层循环
}
}
template<class RandomAccessIterator>
inline void InnerInsert(RandomAccessIterator first,RandomAccessIterator last)
{
typedef typename iterator_traits<RandomAccessIterator>::value_type ValueType;
ValueType value = *last;
if(value < *first) //小于最前端的数,则整体后移
{
copy_backward(first,last,last+1);
*first = value;
}
else
{
UnguardedInnerInsert(last,value);
}
}
template<class RandomAccessIterator,class T>
inline void UnguardedInnerInsert(RandomAccessIterator last,T value)
{
RandomAccessIterator next = last;
--next;
while(value < *next)
{
*last = *next;
last = next;
--next;
}
*last = value;
}
using namespace std;
template<class RandomAccessIterator,class T>//插入排序的辅助函数1
inline void InnerInsert(RandomAccessIterator first,RandomAccessIterator last,T *);
template<class RandomAccessIterator,class T>//插入排序的辅助函数2
inline void UnguardedInnerInsert(RandomAccessIterator last,T value);
template<class RandomAccessIterator>
void InsertSort(RandomAccessIterator first,RandomAccessIterator last)
{
if(first == last)
return;
for(RandomAccessIterator iter = first + 1;iter != last;++iter)//外层循环
{
InnerInsert(first,iter);//内层循环
}
}
template<class RandomAccessIterator>
inline void InnerInsert(RandomAccessIterator first,RandomAccessIterator last)
{
typedef typename iterator_traits<RandomAccessIterator>::value_type ValueType;
ValueType value = *last;
if(value < *first) //小于最前端的数,则整体后移
{
copy_backward(first,last,last+1);
*first = value;
}
else
{
UnguardedInnerInsert(last,value);
}
}
template<class RandomAccessIterator,class T>
inline void UnguardedInnerInsert(RandomAccessIterator last,T value)
{
RandomAccessIterator next = last;
--next;
while(value < *next)
{
*last = *next;
last = next;
--next;
}
*last = value;
}
- 高效的插入排序
- 归并排序、希尔排序、插入排序、快速排序-绝对的高效优化的代码
- 插入排序高效改进之希尔排序
- 排序的一种高效实现:分治排序与插入排序混合使用
- 排序算法:希尔排序(更高效的插入法排序)
- 归并排序,选择排序,插入排序,高效插入排序,快速排序
- LeetCode 147. Insertion Sort List插入排序链表的高效简单解法
- 插入排序和插入排序的改进
- java数组的高效排序
- MySQL高效的批插入 BULK INSERT
- 大批量高效插入数据库的方法
- sqlite高效的批量数据插入sqlite3_prepare_v2
- C#中高效的数据插入方法
- 高效实现红黑树的插入--c实现
- 排序的之插入排序
- 简单的排序---插入排序
- 插入排序的实现
- 单链表的插入排序
- 求指教
- wm_concat实例 行转列
- Andriod的联通性---Wi-Fi Direct(一)
- linux脚本----date的使用
- 经典排序算法之归并排序
- 高效的插入排序
- 这样买票很方便
- QT网上学习资源+查找文档
- 不去火车站也能买到火车票
- [C/C++标准库]_[初级]_[C++ iostream read getline 读取文件慢的原因]
- 重力感应器G—sensor 驱动分析
- S3C2440---点亮第一盏灯
- 单页应用详解——(4)测试
- myeclipse+8.6各个版本注册码