C++STL之希尔排序

/*****************************
*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;
	}
}


你可能感兴趣的:(C++,算法,希尔排序,STL)