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

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

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

    無為

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

      BlogJava :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
      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;
          // 找到最大的子節(jié)點(diǎn)
          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//  判斷是否輸出結(jié)束
            break;

          if (++column == itemsPerRow// 是否到達(dá)行尾?
          {
            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();
      }
    }



    凡是有該標(biāo)志的文章,都是該blog博主Caoer(草兒)原創(chuàng),凡是索引、收藏
    、轉(zhuǎn)載請(qǐng)注明來處和原文作者。非常感謝。

    posted on 2007-09-28 14:24 草兒 閱讀(1538) 評(píng)論(0)  編輯  收藏 所屬分類: java
    主站蜘蛛池模板: 99视频在线看观免费| 国产精品极品美女免费观看| 亚洲人配人种jizz| 国产精品免费视频播放器| 国产久爱免费精品视频| 亚洲视频网站在线观看| 国产最新凸凹视频免费| 成全视频高清免费观看电视剧| 亚洲日本香蕉视频| 亚洲午夜无码AV毛片久久| 最近免费中文字幕大全高清大全1| 亚洲国产欧洲综合997久久| 亚洲日韩国产精品第一页一区| 在线观看av永久免费| 成人av片无码免费天天看| 亚洲xxxx视频| 亚洲国产精品无码av| 暖暖日本免费在线视频| 日本不卡免费新一区二区三区| 亚洲AV日韩综合一区| 亚洲国产精品久久久久网站 | 91香蕉国产线观看免费全集| 亚洲JLZZJLZZ少妇| 亚洲制服中文字幕第一区| 亚洲国产成人五月综合网 | 日本特黄特色AAA大片免费| 4480yy私人影院亚洲| 亚洲精品无码你懂的网站| 日本阿v免费费视频完整版| a级毛片在线免费| 国产亚洲人成在线影院| 亚洲av日韩av综合| 亚洲AV中文无码乱人伦下载| 无码欧精品亚洲日韩一区夜夜嗨| 91在线视频免费播放| 无码精品人妻一区二区三区免费看| 色吊丝性永久免费看码 | 日本免费xxxx| 野花香高清在线观看视频播放免费| 男男黄GAY片免费网站WWW| 国产精品亚洲片在线va|