希尔排序(c++

#include 
#include 

using namespace std;

void ShellSort(vector& vec)
{
	int i,gap=vec.size();
	while (gap > 1)
	{
		gap /= 2;
		for (i = 0; i < vec.size() - gap; i++)
		{
			int temp = vec[i + gap];
			int j = i;
			while (j >= 0)
			{
				if (vec[j] >= temp)
				{
					vec[j + gap] = vec[j];
					j-=gap;
				}
				else
					break;
			}
			vec[j + gap] = temp;
		}
	}
}

void PrintArray(vector vec)
{
	for (int num : vec)
	{
		cout << num << " ";
	}
}

void Test()
{
	vector a;
	int n;
	while (cin >> n)
	{
		a.push_back(n);
	}
	ShellSort(a);
	PrintArray(a);
}

int main()
{
	Test();
	return 0;
}

你可能感兴趣的:(c++,算法,数据结构)