#include <iostream>
#include <iomanip>
using namespace std;
void swap(int &x, int &y)
{
int temp = x;
x = y;
y = temp;
}
void shellsort(int a[], int sz)
{
int i, j, gap;
for (gap = sz / 2; gap > 0; gap /= 2)
for (i = gap; i < sz; i++)
for (j = i - gap; j >= 0 && a[j] > a[j + gap]; j -= gap)
swap(a[j], a[j + gap]);
}
int main()
{
int a[] = { 15, 9, 8, 1, 4, 11, 7, 12, 13, 6, 5, 3, 16, 2, 10, 14};
int size = sizeof(a)/sizeof(int);
shellsort(a, size);
for (int i = 0; i < size; i++) cout << setw(3) << a[i];
cout<<endl;
return 0;
}
希尔排序
最新推荐文章于 2024-06-04 00:42:18 发布