無為

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

            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é)點
                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// 是否到達行尾?
                {
                  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(草兒)原創(chuàng),凡是索引、收藏
          、轉(zhuǎn)載請注明來處和原文作者。非常感謝。

          posted on 2007-09-28 14:24 草兒 閱讀(1545) 評論(0)  編輯  收藏 所屬分類: java
          主站蜘蛛池模板: 徐州市| 石河子市| 巫溪县| 佳木斯市| 集贤县| 攀枝花市| 兴安盟| 长寿区| 福州市| 湟中县| 桦川县| 汝州市| 田阳县| 息烽县| 夹江县| 南漳县| 上思县| 永川市| 都江堰市| 中江县| 双峰县| 岳阳市| 莒南县| 天镇县| 平乡县| 搜索| 永寿县| 习水县| 太湖县| 博乐市| 洪湖市| 临西县| 渑池县| 乐亭县| 和平县| 张家川| 桐城市| 从江县| 华亭县| 龙井市| 孟津县|