排序算法:6.插入排序之希尔排序

 排序算法:6.插入排序之希尔排序_第1张图片

#include
using namespace std;
#include
#include
#include
#include
#define BUFFER_SIZE 256
void shellsort(int a[], int n)
{
	int gap = n / 2;

	while (gap >= 1)
	{
		for (int i = gap; i < n; ++i)
		{
			int j = i - gap;
			int get = a[i];
			while (j >= 0 && a[j] > get)
			{
				a[j + gap] = a[j];
				j = j - gap;
			}
			a[j + gap] = get;
		}
		gap = gap / 2;                    // 递减增量
	}
}
void  main()
{
	int a[] = { 16,25,12,30,47,11, 23,36,9,18,31 };

	int n = sizeof(a) / sizeof(a[0]);
	shellsort(a, n);
	for (int i = 0; i < n; ++i)
	{
		cout << a[i] << " " << endl;
	}
}

 

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