<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    無為

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

      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 草兒 閱讀(1538) 評論(0)  編輯  收藏 所屬分類: java
    主站蜘蛛池模板: 国产成人亚洲影院在线观看| 午夜爱爱免费视频| 亚洲精品~无码抽插| 黄色大片免费网站| 亚洲熟伦熟女新五十路熟妇| 高清免费久久午夜精品| 亚洲一区二区三区乱码A| aaa毛片免费观看| 亚洲国产成人精品无码区在线观看| 免费福利电影在线观看| 久久亚洲精品成人777大小说| 人人揉揉香蕉大免费不卡| 亚洲综合国产精品| 美女视频黄是免费的网址| 亚洲欧美乱色情图片| 亚洲国产日韩成人综合天堂| 久久性生大片免费观看性| 亚洲欧洲日韩不卡| 成人免费午夜无码视频| 国产亚洲高清在线精品不卡| 在线亚洲97se亚洲综合在线| 99久久免费看国产精品| 伊人久久亚洲综合影院首页| jjzz亚洲亚洲女人| 无码国产精品一区二区免费3p| 亚洲精品午夜国产va久久| 亚洲国产精品综合久久网络 | 亚洲成av人在线观看网站| 亚洲第一区精品日韩在线播放| 国产午夜无码精品免费看| 亚洲人成在线免费观看| va亚洲va日韩不卡在线观看| 久久久免费的精品| 亚洲av综合av一区二区三区| 亚洲国产精品国自产拍AV| 女人18特级一级毛片免费视频| 一个人看的免费视频www在线高清动漫 | 性无码免费一区二区三区在线| 波多野结衣亚洲一级| 久久精品夜色噜噜亚洲A∨| 99热这里只有精品6免费|