排序(之希爾排序)

來源:互聯網
上載者:User

希爾排序是不穩定排序,平均演算法複雜度和最壞演算法複雜度為:O(nlgn),最好的情況為O(n);空間複雜度為O(1);

希爾排序是插入排序的一種,且希爾排序的演算法複雜度大大低於插入排序,只是由穩定變為不穩定的排序。

#include<stdio.h>void Shell_Insert(int a[],int d,int n){int i,j,rc;for(i=d;i<n;i++){if(a[i]<a[i-d]){rc=a[i];j=i;while(j-d>=0 && rc<a[j-d]){a[j]=a[j-d];j-=d;}a[j]=rc;}}}void ShellSort(int a[],int n){int increment=n;//增量do{increment=increment/3+1;Shell_Insert(a,increment,n);}while(increment>1);}void main(){int a[]={49,38,65,97,76,13,27,49,55,04};int n=sizeof(a)/sizeof(a[0]);ShellSort(a,n);for(int i=0;i<n;i++){printf("%d\n",a[i]);}}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.