C語言 指標練習-希爾排序法

來源:互聯網
上載者:User
#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);
}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.