JAVA排序演算法實現代碼-堆(Heap)排序
- /**
- * 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("");
- }
- }
- }
/** * 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