無為

          無為則可為,無為則至深!

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            190 Posts :: 291 Stories :: 258 Comments :: 0 Trackbacks
          Heap sort
                 
          import java.io.IOException;

          class MyNode {
            private int iData; 

            public MyNode(int key) {
              iData = key;
            }

            public int getKey() {
              return iData;
            }

          }

          public class Heap {
            private MyNode[] heapArray;

            private int maxSize;

            private int currentSize; // number of items in array

            public Heap(int mx) {
              maxSize = mx;
              currentSize = 0;
              heapArray = new MyNode[maxSize];
            }

            public MyNode remove() 
            
              MyNode root = heapArray[0];
              heapArray[0= heapArray[--currentSize];
              trickleDown(0);
              return root;
            }

            public void trickleDown(int index) {
              int largerChild;
              MyNode top = heapArray[index]
              while (index < currentSize / 2)
              {
                int leftChild = * index + 1;
                int rightChild = leftChild + 1;
                // 找到最大的子節點
                if (rightChild < currentSize
                    && 
                    heapArray[leftChild].getKey() < heapArray[rightChild]
                        .getKey())
                  largerChild = rightChild;
                else
                  largerChild = leftChild;

                if (top.getKey() >= heapArray[largerChild].getKey())
                  break;

                heapArray[index= heapArray[largerChild];
                index = largerChild; 
              }
              heapArray[index= top;
            }

            public void displayHeap() {
              int nBlanks = 32;
              int itemsPerRow = 1;
              int column = 0;
              int currentIndex = 0
              while (currentSize > 0)
              {
                if (column == 0
                  for (int k = 0; k < nBlanks; k++)
                    System.out.print(' ');
                System.out.print(heapArray[currentIndex].getKey());

                if (++currentIndex == currentSize//  判斷是否輸出結束
                  break;

                if (++column == itemsPerRow// 是否到達行尾?
                {
                  nBlanks /= 2
                  itemsPerRow *= 2
                  column = 0
                  System.out.println()
                else
                  for (int k = 0; k < nBlanks * 2; k++)
                    System.out.print(' ')// 輸入空白
              
            }

            public void displayArray() {
              for (int j = 0; j < maxSize; j++)
                System.out.print(heapArray[j].getKey() " ");
              System.out.println("");
            }

            public void insertAt(int index, MyNode newNode) {
              heapArray[index= newNode;
            }

            public void incrementSize() {
              currentSize++;
            }

            public static void main(String[] argsthrows IOException {
              int size, i;

              size = 100;
              Heap theHeap = new Heap(size);

              for (i = 0; i < size; i++) {
                int random = (int) (java.lang.Math.random() 100);
                MyNode newNode = new MyNode(random);
                theHeap.insertAt(i, newNode);
                theHeap.incrementSize();
              }

              System.out.print("Random: ");
              theHeap.displayArray();
              for (i = size / 1; i >= 0; i--)
                theHeap.trickleDown(i);

              System.out.print("Heap:   ");
              theHeap.displayArray();
              theHeap.displayHeap();
              for (i = size - 1; i >= 0; i--) {
                MyNode biggestNode = theHeap.remove();
                theHeap.insertAt(i, biggestNode);
              }
              System.out.print("Sorted: ");
              theHeap.displayArray();
            }
          }



          凡是有該標志的文章,都是該blog博主Caoer(草兒)原創,凡是索引、收藏
          、轉載請注明來處和原文作者。非常感謝。

          posted on 2007-09-28 14:24 草兒 閱讀(1540) 評論(0)  編輯  收藏 所屬分類: java
          主站蜘蛛池模板: 屏南县| 呼伦贝尔市| 海宁市| 和顺县| 东宁县| 达孜县| 固安县| 涪陵区| 榆社县| 淅川县| 丘北县| 鸡东县| 自治县| 淮阳县| 襄汾县| 武城县| 稻城县| 建水县| 平昌县| 即墨市| 迁西县| 哈巴河县| 霍城县| 湘乡市| 茶陵县| 娄底市| 长春市| 德江县| 岱山县| 略阳县| 遂平县| 红桥区| 南丰县| 青海省| 锦屏县| 青铜峡市| 阳曲县| 张家界市| 梓潼县| 延边| 孟村|