#include<iostream>
using namespace std;
void ShellInsert(int *a, int length, int n)
{
int i, j;
for (i = n + 1; i <= length; i++)
{
if (a[i] < a[i - n])
{
a[0] = a[i]; //备份a[i]
for (j = i - n; j>0 && a[0] < a[j]; j -= n)
{
a[j + n] = a[j];
}
a[j + n] = a[0];
}
}
}
void ShellSort(int *a, int length, int n)//n为增量
{
int i;
for (i = n; i > 0; i--)
ShellInsert(a, length, i);
}
int main()
{
int a[] = { 0, 23, 14, 56, 18, 2, 6, 34, 12 };
int length = sizeof(a) / sizeof(a[0]) - 1;
ShellSort(a, length, 3);
for (int i = 1; i <= length; i++)
{
printf("%d ", a[i]);
}
return 0;
}
希尔排序
最新推荐文章于 2024-08-30 21:09:25 发布