排序——3.希爾排序

來源:互聯網
上載者:User

希爾排序是在直接插入排序上改進而來。

1.以一個增量temp將要排序的數組進行分組,所以距離為temp的倍數的元素在同一個組;

2.對每一個組進行快速插入排序;

3.改變增量,從複以上操作;

4.直至增量等於1,結束。

實現代碼如下:

//希爾排序<br />public class Demo32<br />{<br />public static void main(String args[]){<br />int r[]={2,4,8,1,5,6,3,9,10,5,0,4,7};<br />fun(r);<br />for(int i=0;i<r.length;i++){<br />System.out.print(r[i]+",");<br />}<br />int i=1;<br />}<br />public static void fun(int intArray[]){<br />int temp = intArray.length/2 ; //聲明增量,以此為間隔將元素分組<br />int var;<br />while(temp>0){<br />for(int i=temp;i<intArray.length;i++){ //對所有相隔為temp的元素進行直接插入排序<br />var = intArray[i];<br />int j=i-temp;<br />while(j>=0&&intArray[j]>var){<br />intArray[j+temp]=intArray[j];<br />j=j-temp;<br />}<br />intArray[j+temp]=var;<br />}<br />temp=temp/2; //改變增量</p><p>}<br />}<br />}

聯繫我們

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