#include <stdio.h>
void print_result(float *,int);
void Shell_Sort(float *,int);
int main()
{
int i;
float array[10];
float * pointer;
printf("請輸入10個數:\n");
for(i=0;i<10;i++)
{
scanf("%f",&array[i]);
}
pointer=array;
Shell_Sort(pointer,10);
print_result(pointer,10);
return 0;
}
void print_result(float *p,int n)
{ //輸出結果
int k;
for(k=0;k<n;k++)
{
printf("%g\t",*(p+k));
}
}
void Shell_Sort(float *pt,int n)
{//全用指標的希爾排序法
int d=n; //增量初值
int i,j;
float tempnum;
do
{
d/=2; //求下一增量
//以下為一趟增量為d的希爾插入排序;
for(i=d; i<n;i++) //將array[d+1,...n]分別插入各組當前的有序區
{
if(*(pt+i) > *(pt+i-d)){
tempnum=*(pt+i); j=i-d; //tempnum只是暫存單元,不是哨兵;
do{ //尋找a[i]的插入位置
*(pt+j+d)=*(pt+j) ; //後移記錄
j-=d; //尋找前一記錄
}while(j>=0&&tempnum>*(pt+j));
*(pt+j+d)=tempnum; //插入array[i]到正確的位置上
}
}
}while(d>1);
}
附加以前做的希爾排序圖及代碼:
////=======================================
void Shellpass(int a[],int d,int n)
{//希爾排序法中的一趟排序,d為增量
int i,j,t;
int c;
for(i=d; i<n;i++) //將a[d+1,...n]分別插入各組當前的有序區
{
if(a[i]<a[i-d]){
t=a[i]; j=i-d; //t只是暫存單元,不是哨兵;
do{ //尋找a[i]的插入位置
a[j+d]=a[j]; //後移記錄
j-=d; //尋找前一記錄
}while(j>=0&&t<a[j]);
a[j+d]=t; //插入a[i]到正確的位置上
c=' ';
}
else
{
c='.';
}
int l;
for(l=0;l<n;l++)
{
printf("%3c%c",a[l],c);
}
printf("\n");
}
printf("\n");
}
void ShellSort(int a[],int n)
{
printf("\n希爾排序法過程:\n");
int increment=n; //增量初值
do
{
increment/=2; //求下一增量
Shellpass(a,increment,n); //一趟增量為increment的希爾插入排序;
}while(increment>1);
}