C++STL之希尔排序
2018-07-20 来源:open-open
/*****************************
*shellSort.h
*****************************/
#include "stdafx.h"
#include <vector>
using namespace std;
template <typename T>
void shellSort(vector<T>& vec)
{
int gap = vec.size()/2;
for (; gap != 0; gap = gap/2)
{
shellSort(vec, gap); //以gap步长为参数进行排序
}
}
template <typename T>
void shellSort(vector<T>& vec, int gap)
{
int i = gap;
for (; i < vec.size(); i++) // 从gap 到 vec.size()之间
{
//这种循环交换的思想,省去每次比较都要交换的次数
T tempValue = vec[i];
int j = i;
for (; j >= gap && tempValue < vec[j - gap] ; j = j - gap) //这里其实进行的是倒序循环
{
vec[j] = vec[j - gap];
}
vec[j] = tempValue;
}
}
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。
上一篇: C#访问文件路径通用类
下一篇:繁体转换成简体
最新资讯
热门推荐