日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

Java数据结构与排序算法——堆和堆排序

發布時間:2025/3/19 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java数据结构与排序算法——堆和堆排序 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?

//================================================= // File Name : Heap_demo //------------------------------------------------------------------------------ // Author : Common//類名:Node_Heap //屬性: //方法: class Node_Heap{public int iData;public Node_Heap(int iData) { //構造函數super();this.iData = iData;}public int getiData() {return iData;}public void setiData(int iData) {this.iData = iData;} }//類名:Heap //屬性: //方法: class Heap{private Node_Heap[] heapArray;public int maxSize;private int currentSize;public Heap(int maxSize) { //構造函數super();this.maxSize = maxSize;this.currentSize = 0;heapArray = new Node_Heap[maxSize];}public boolean isEmpty(){return currentSize ==0;}public boolean insert(int key){if(currentSize == maxSize){return false;}Node_Heap newNode = new Node_Heap(key); heapArray[currentSize] = newNode; //把插入的節點放在最后的位置trickleUp(currentSize++); //插入節點并把currentSize加1return true;}//用于插入,把父類節點下移,然后把插入的節點放到合適的位置public void trickleUp(int index){int parent = (index-1)/2;Node_Heap bottom = heapArray[index]; //暫存新插入的節點,因為需要把父節點下移while(index>0 && heapArray[parent].getiData()<bottom.getiData()){ //如果小,就下移heapArray[index] = heapArray[parent]; //把父類節點下移index = parent; //用于遞歸parent = (parent-1)/2;}heapArray[index] = bottom; //把插入的節點放到合適的位置}public Node_Heap remove(){ //刪除最大的節點Node_Heap root = heapArray[0];heapArray[0]=heapArray[--currentSize];trickleDown(0);return root;}//用于刪除,把子類節點上移public void trickleDown(int index){int largerChild;Node_Heap top = heapArray[index]; //while(index<currentSize/2){ //如果小,就下移int leftChild = 2*index+1;int rightChild = leftChild+1;if(rightChild<currentSize && heapArray[leftChild].getiData() < heapArray[rightChild].getiData())largerChild = rightChild;elselargerChild = leftChild;if(top.getiData()>=heapArray[largerChild].getiData())break;heapArray[index] = heapArray[largerChild];index = largerChild;}heapArray[index] = top; }public void displayHeap(){System.out.print("heapArray:");for(int i=0;i<heapArray.length;i++){if(heapArray[i] != null)System.out.print(heapArray[i].getiData()+" ");elseSystem.out.print(" -- ");}System.out.println();int nBlanks = 32; //定義空格int itemsPerRow = 1;int column = 0;int j=0; //標記當前的數組下標,從0開始System.out.println("......................................................");while(currentSize > 0){if(column == 0){for(int i=0;i<nBlanks;i++){System.out.print(" ");}}System.out.print(heapArray[j].getiData());if(++j == currentSize){break;}if(++column==itemsPerRow){ //如果每一行計數等于這一行的上限,則換行nBlanks /= 2; //空格數減半itemsPerRow *= 2; //每一行的上限column = 0;System.out.println();}else{for(int i=0;i<nBlanks*2-2;i++){System.out.print(" ");}}}System.out.println("\n"+"......................................................");}}//主類 //Function : Heap_demo public class Heap_demo {public static void main(String[] args) {// TODO 自動生成的方法存根int anArrays[]={1,2,3,4,5,6,7,8,9,10};Heap theHeap = new Heap(31); // theHeap.insert(1); // theHeap.insert(2); // theHeap.insert(3); // theHeap.insert(4); // theHeap.insert(5); // theHeap.insert(6);for(int i=0;i<10;i++){theHeap.insert(anArrays[i]);}theHeap.displayHeap();//theHeap.remove();//theHeap.displayHeap();for(int i=0;i<10;i++){anArrays[i]=theHeap.remove().iData;}for(int i=0;i<10;i++){System.out.print(anArrays[i]+" ");}}}

?

總結

以上是生活随笔為你收集整理的Java数据结构与排序算法——堆和堆排序的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。