#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
void Shell_Sort(int a[], int n)
{
int h,i,j,temp;
for (h=n/2; h>0; h=h/2)
{
for (i=h; i<n; i++)
{
temp = a[i];
for (j=i-h; j>=0 && temp < a[j]; j-=h)
{
a[j+h] = a[j];
}
a[j+h] = temp;
}
}
}
void print(int a[],int n)
{
for(int i=0; i<n; i++)
{
cout<<a[i]<<" ";
}
cout << endl;
}
void main()
{
int a[10];
srand((unsigned)time(NULL));//初始化随机数
for(int i=0; i<10; i++)
{
a[i]=rand()%20;
}
cout << "排序前:";
print(a,sizeof(a)/sizeof(a[0]));
int n=sizeof(a)/sizeof(a[0]);
Shell_Sort(a,n);
cout << "排序后:";
print(a,sizeof(a)/sizeof(a[0]));
}
【基础排序】希尔排序
最新推荐文章于 2024-10-09 17:52:59 发布