JAVA排序演算法實現代碼-堆(Heap)排序

來源:互聯網
上載者:User

JAVA排序演算法實現代碼-堆(Heap)排序
  1. /** 
  2.  * JAVA排序演算法實現代碼-堆(Heap)排序。 
  3.  *  
  4.  * @author 老紫竹 JAVA世紀網(java2000.net) 
  5.  *  
  6.  */  
  7. public class Test {  
  8.   public static int[] Heap = { 10, 32, 1, 9, 5, 7, 12, 0, 4, 3 }; // 預設資料數組  
  9.   
  10.   public static void main(String args[]) {  
  11.     int i; // 迴圈計數變數  
  12.     int Index = Heap.length; // 資料索引變數  
  13.   
  14.     System.out.print("排序前: ");  
  15.     for (i = 1; i < Index - 1; i++)  
  16.       System.out.printf("%3s", Heap[i]);  
  17.     System.out.println("");  
  18.   
  19.     HeapSort(Index - 2); // 堆排序  
  20.   
  21.     System.out.print("排序後: ");  
  22.     for (i = 1; i < Index - 1; i++)  
  23.       System.out.printf("%3s", Heap[i]);  
  24.     System.out.println("");  
  25.   }  
  26.   
  27.   /** 
  28.    * 建立堆 
  29.    */  
  30.   public static void CreateHeap(int Root, int Index) {  
  31.     int i, j; // 迴圈計數變數  
  32.     int Temp; // 暫存變數  
  33.     int Finish; // 判斷堆是否建立完成  
  34.   
  35.     j = 2 * Root; // 子節點的Index  
  36.     Temp = Heap[Root]; // 暫存Heap的Root 值  
  37.     Finish = 0; // 預設堆建立尚未完成  
  38.   
  39.     while (j <= Index && Finish == 0) {  
  40.       if (j < Index) // 找最大的子節點  
  41.         if (Heap[j] < Heap[j + 1])  
  42.           j++;  
  43.       if (Temp >= Heap[j])  
  44.         Finish = 1; // 堆建立完成  
  45.       else {  
  46.         Heap[j / 2] = Heap[j]; // 父節點 = 目前節點  
  47.         j = 2 * j;  
  48.       }  
  49.     }  
  50.     Heap[j / 2] = Temp; // 父節點 = Root值  
  51.   }  
  52.   
  53.   public static void HeapSort(int Index) {  
  54.     int i, j, Temp;  
  55.     // 將二叉樹轉成Heap  
  56.     for (i = (Index / 2); i >= 1; i--)  
  57.       CreateHeap(i, Index);  
  58.   
  59.     // 開始進行堆排序  
  60.     for (i = Index - 1; i >= 1; i--) {  
  61.       Temp = Heap[i + 1]; // Heap的Root值和最後一個值交換  
  62.       Heap[i + 1] = Heap[1];  
  63.       Heap[1] = Temp;  
  64.       CreateHeap(1, i); // 對其餘數值重建堆  
  65.   
  66.       System.out.print("排序中: ");  
  67.       for (j = 1; j <= Index; j++)  
  68.         System.out.printf("%3s",Heap[j]);  
  69.       System.out.println("");  
  70.     }  
  71.   }  
  72. }  
/** * JAVA排序演算法實現代碼-堆(Heap)排序。 *  * @author 老紫竹 JAVA世紀網(java2000.net) *  */public class Test {  public static int[] Heap = { 10, 32, 1, 9, 5, 7, 12, 0, 4, 3 }; // 預設資料數組  public static void main(String args[]) {    int i; // 迴圈計數變數    int Index = Heap.length; // 資料索引變數    System.out.print("排序前: ");    for (i = 1; i < Index - 1; i++)      System.out.printf("%3s", Heap[i]);    System.out.println("");    HeapSort(Index - 2); // 堆排序    System.out.print("排序後: ");    for (i = 1; i < Index - 1; i++)      System.out.printf("%3s", Heap[i]);    System.out.println("");  }  /**   * 建立堆   */  public static void CreateHeap(int Root, int Index) {    int i, j; // 迴圈計數變數    int Temp; // 暫存變數    int Finish; // 判斷堆是否建立完成    j = 2 * Root; // 子節點的Index    Temp = Heap[Root]; // 暫存Heap的Root 值    Finish = 0; // 預設堆建立尚未完成    while (j <= Index && Finish == 0) {      if (j < Index) // 找最大的子節點        if (Heap[j] < Heap[j + 1])          j++;      if (Temp >= Heap[j])        Finish = 1; // 堆建立完成      else {        Heap[j / 2] = Heap[j]; // 父節點 = 目前節點        j = 2 * j;      }    }    Heap[j / 2] = Temp; // 父節點 = Root值  }  public static void HeapSort(int Index) {    int i, j, Temp;    // 將二叉樹轉成Heap    for (i = (Index / 2); i >= 1; i--)      CreateHeap(i, Index);    // 開始進行堆排序    for (i = Index - 1; i >= 1; i--) {      Temp = Heap[i + 1]; // Heap的Root值和最後一個值交換      Heap[i + 1] = Heap[1];      Heap[1] = Temp;      CreateHeap(1, i); // 對其餘數值重建堆      System.out.print("排序中: ");      for (j = 1; j <= Index; j++)        System.out.printf("%3s",Heap[j]);      System.out.println("");    }  }}

運行結果

排序前: 32 1 9 5 7 12 0 4

排序中: 12 7 9 5 1 4 0 32

排序中: 9 7 4 5 1 0 12 32

排序中: 7 5 4 0 1 9 12 32

排序中: 5 1 4 0 7 9 12 32

排序中: 4 1 0 5 7 9 12 32

排序中: 1 0 4 5 7 9 12 32

排序中: 0 1 4 5 7 9 12 32

排序後: 0 1 4 5 7 9 12 32

聯繫我們

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