堆排序過程

來源:互聯網
上載者:User

標籤:構造   最大   class   基礎上   篩選   迴圈   word   title   tail   

原文連結:http://blog.csdn.net/sxhelijian/article/details/50295637

例:對{57, 40, 38, 11, 13, 34, 48, 75, 6, 19, 9, 7}進行堆排序的過程。

演算法如下:

void HeapSort(RecType R[],int n){    int i;    RecType temp;    //(1)迴圈建立初始堆    for (i=n/2; i>=1; i--) sift(R,i,n); //(2)進行n-1次迴圈,完成推排序 for (i=n; i>=2; i--) { temp=R[1]; //將第一個元素同當前區間內R[1]對換 R[1]=R[i]; R[i]=temp; sift(R,1,i-1); //篩選R[1]結點,得到i-1個結點的堆 }}

 

(1)迴圈建立初始堆

    for (i=n/2; i>=1; i--) sift(R,i,n);

 

用給出的序列構造堆的初始狀態如下: 
 
在此基礎上,根據上述代碼,從最後一個分支節點開始調整,目標是得到大根堆。過程如: 
 
這個堆的儲存結構是: 

(2)進行n-1次迴圈,完成推排序

    for (i=n; i>=2; i--) { temp=R[1]; //最大值交換到最後 R[1]=R[i]; R[i]=temp; sift(R,1,i-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.