现在的位置: 首页 > 综合 > 正文

希尔排序

2017年11月15日 ⁄ 综合 ⁄ 共 538字 ⁄ 字号 评论关闭
#include <iostream>
#include <vector>
using namespace std;

void swap(int & a,int & b)
{
	a=a+b;
	b=a-b;
	a=a-b;
}
void print(vector<int> & v)
{
	vector<int>::iterator pos;
	for(pos = v.begin();pos!=v.end();pos++)
		cout<<*pos<<" ";
	cout<<endl;
}

vector<int> & shellSort(vector<int> & v)
{
	int n=v.size();
	for(auto dk =n/2;dk>0;dk=dk/2)
		for(auto i=dk;i<n;i++)
		{
			auto j=i-dk;
			int temp =v[i] ;
			while(j>=0&&v[j]>temp)
			{
				v[j+dk] =v[j];
				j-=dk;
			}
			v[j+dk] =temp;
		}
	return v;
}



void main()
{
	const int n=100;
	vector<int> v;
	v.reserve(n);
	for(int i =0;i<n;i++)
		v.push_back(rand()%100);
	print(v);
	print(shellSort(v));
	system("pause");
}

抱歉!评论已关闭.