我的“堆排序演算法”實現

來源:互聯網
上載者:User

這是我的“堆排序演算法”Java實現!

View Code

  1 /*  2  * 目的:實現堆排序演算法  3  * 注意:實際上沒有heap這種記憶體資料結構,我們通過Array或LinkList來類比,人為地看成是heap結構  4  * 功能:對一堆數進行非升排序  5  * 作者:陳沛銳  6  * 時間:2013.04.02  7  *   8  * 經驗積累:  9  * 1.Random() seed 10  * 2.Array.length是Array中元素的個數,即Array中有多少個元素,例如,A的最大下標為6,則A.length=7;在定義數組時,int[] A=new int[length]; 11  * 3.Random() 產生任意區間範圍的隨機數(包括小數) 12  * 4.Math.Random(); 13  */ 14 package part02.chapter06; 15  16 import java.util.Random; 17 import java.util.Scanner; 18  19 public class _1exercise { 20  21     /** 22      * @param args 23      */ 24     public static void main(String[] args) { 25         Random ran = new Random(); 26         System.out.println("請輸入您要產生的資料量(用整數表示):"); 27         Scanner myScanner = new Scanner(System.in);// 記得關閉資源 28         int A_length = myScanner.nextInt(); 29         int[] A = new int[A_length]; 30         // System.out.println(A.length);7 記得總結經驗和注意點 31         System.out.println("請輸入您要產生的資料的最大值:"); 32         int A_max = myScanner.nextInt(); 33         System.out.println("請輸入您要產生的資料的最小值:"); 34         int A_min = myScanner.nextInt(); 35         System.out.println("隨機產生的資料如下:"); 36         for (int i = 0; i < A_length; i++) { 37             //注意A_max-A_min後要+1才能包括最大值, 38             //因為nextInt()產生的是不包括參數的0-參數+1範圍裡的隨機數 39             A[i] = ran.nextInt(A_max - A_min + 1) + A_min; 40             System.out.print(A[i] + " "); 41         } 42         System.out.println(); 43         HEAPSORT heapSort = new HEAPSORT(); 44         heapSort.sort(A, 0, A_length - 1);// 注意A_length-1 45         System.out.println("通過堆排序對資料進行排序後的資料如下:"); 46         for (int i = 0; i < A_length; i++) { 47             System.out.print(A[i] + " "); 48         } 49         // 關閉資源 50         if (myScanner != null) { 51             myScanner.close(); 52         } 53     } 54  55 } 56  57 // 堆排序類 58 class HEAPSORT { 59     int heap_length = 0; 60  61     public void sort(int[] A, int p, int r) { 62         heap_length = r - p + 1; 63         build_heap(A, p, r); 64         for (int i = r; i > p; i--) { 65             int temp = A[0]; 66             A[0] = A[i]; 67             A[i] = temp; 68             heap_length--; 69             max_heap(A, 0); 70         } 71     } 72  73     // 建堆 74     public void build_heap(int[] A, int p, int r) { 75         for (int i = (heap_length - 1) / 2; i >= 0; i--) { 76             max_heap(A, i); 77         } 78         System.out.println("將資料進行建堆後如下:"); 79         for (int i = 0; i < A.length; i++) { 80             System.out.print(A[i] + " "); 81         } 82         System.out.println(); 83     } 84  85     // 使以i節點為根節點的堆成為最大堆 86     public void max_heap(int[] A, int i) { 87         // 得到i左右子節點的下標 88         // 此處要注意i=0時,i==i_left;i_left==i_right;但是不影響要達到的判斷目的,僅僅是命名表示不形象 89         int i_left = i * 2; 90         int i_right = i * 2 + 1; 91         // 取出三個節點中最大值的下標 92         int max_sub = i;// max_sub = i != 93                         // 0;因為當i>(heap_length-1)/2時,一下if語句不執行,下一個if語句中max_sub!=i並且使該方法退出才正確 94         if (i <= (heap_length - 1) / 2) { 95             if (i_left < heap_length) {// 不是i_left <= heap_length && 96                                         // heap_length-1 != heap_length 97                 if (A[i_left] > A[i]) { 98                     max_sub = i_left; 99                 }100             }101             if (i_right < heap_length) {// 不是i_right <= heap_length102                 if (A[max_sub] < A[i_right]) {103                     max_sub = i_right;104                 }105             }106         }107         // 將三個節點中的最大值與A[i]互換,並遞迴調用108         int temp = 0;109         if (max_sub != i) {110             temp = A[max_sub];111             A[max_sub] = A[i];112             A[i] = temp;113             max_heap(A, max_sub);114         }115     }116 }117 /**118  * a sample output:119 請輸入您要產生的資料量(用整數表示):120 10121 請輸入您要產生的資料的最大值:122 10123 請輸入您要產生的資料的最小值:124 0125 隨機產生的資料如下:126 0 0 7 1 9 10 4 2 2 6 127 將資料進行建堆後如下:128 10 9 7 4 6 0 1 2 2 0 129 通過堆排序對資料進行排序後的資料如下:130 0 0 1 2 2 4 6 7 9 10 131 **/

 

聯繫我們

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