aznishboy

HeapSort2

Apr 18th, 2012
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 3.99 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. /**
  4.  * This class implements the PriorityQueue2 interface using
  5.  * a binary heap.
  6.  *
  7.  * the basic idea is that the node whose index is k
  8.  * has children stored with indexes 2*k and 2*k+1 for left/right
  9.  * children, respectively. The root of the binary heap has index 1.
  10.  *
  11.  * @author Your Name
  12.  */
  13.  
  14. public class HeapSort2 implements PriorityQueue2
  15. {
  16.   private List items;
  17.   private int mySize;   // since index 0 is unused, mySize is items.size() - 1
  18.  
  19.   /**
  20.    * Constructs an empty priority queue.
  21.    */
  22.   public HeapSort2()
  23.   {
  24.     items = new ArrayList();
  25.     items.add(null);            // first add should go at index 1 since
  26.                                 // index 0 is unused
  27.     mySize = 0;
  28.   }
  29.  
  30.   public void add(Object obj)
  31.   {
  32.     // PSEUDOCODE:
  33.     // Add 'obj' to end of 'items' list
  34.     // Increment mySize
  35.     // Now 'heap up' the obj added to end of list:
  36.     //   init index k to current index of 'obj'
  37.     //   while (not done && k is not pointing to root, ie k > index 1)
  38.     //   {
  39.     //      if (value at parent node is greater than 'obj')
  40.     //      {
  41.     //          copy value at parent node to k node
  42.     //          move k to parent node
  43.     //      }
  44.     //      else
  45.     //          done -> stop loop
  46.     //   }
  47.     //   set 'heaped up' obj to its final position at latest value of k
  48.   }
  49.  
  50.   public Object removeMin()
  51.   {
  52.     // PSEUDOCODE:
  53.     // if (heap isn't empty)
  54.     // {
  55.     //    fetch min value to be returned
  56.     //    replace min value on the heap with the last element in 'items' list
  57.     //    decrement mySize by 1
  58.     //    if (mySize > 1)
  59.     //       call 'heapify' from root down, where root is at index 1
  60.     //    return min value
  61.     // }
  62.     // return null
  63.     return null;
  64.   }
  65.  
  66.   public Object peekMin()
  67.   {
  68.     // PSEUDOCODE:
  69.     // return value at index 1 of 'items' list
  70.     return null;
  71.   }
  72.  
  73.   public boolean isEmpty()
  74.   {
  75.     // PSEUDOCODE:
  76.     // return true if mySize is zero
  77.     return true;
  78.   }
  79.  
  80.   /**
  81.    * preconditon: subheaps of hroot satisfy heap property (and shape)
  82.    * postcondition: heap rooted at hroot satisfies heap property
  83.    */
  84.   private void heapify(int hroot)
  85.   {
  86.     // SEE BELOW FOR EXAMPLE
  87.     //
  88.     // PSEUDOCODE:
  89.     // 'last' = element to "heap down" i.e. element at index 'hroot'
  90.     // init index k to hroot
  91.     // while (2*k <= mySize)   // index k has at least one child
  92.     // {
  93.     //    // find minimal child, start with left, then check right
  94.     //    child = 2*k   // left child
  95.     //    if (child < mySize)   // k has 2 children
  96.     //        if (right child < left child)
  97.     //           child++    // go to right child instead because it's smaller
  98.     //    // if item to be added <= child, stop and add the item
  99.     //    if (last <= element at 'child')
  100.     //        break         // 'last' is smaller than either child, so leave it alone
  101.     //    else  // 'last' is larger than one or both children
  102.     //    {
  103.     //        copy smallest child to node k
  104.     //        k = child     // now k points to child's index
  105.     //    }
  106.     // }
  107.     // // at this point, element at 'k' has no children OR
  108.     // // is smaller than any of its children -> put 'last' in its final spot at k
  109.     // put 'last' at node k
  110.   }
  111. }
  112.  
  113. /*******************************************************************************
  114.  * HEAPIFY EXAMPLE:
  115.  *
  116.  * Original array in which '13' is being removed as 'min' value and remaining list
  117.  * will be 'heaped down'
  118.  *
  119.  * index:   0   1   2   3   4   5   6   7   8   9   10  11  12  13
  120.  * value:   -   13  18  24  22  38  56  30  34  97  95  83  81  87
  121.  *
  122.  * What specific elements in the array are replaced with:
  123.  *
  124.  * index:   0   1   2   3   4   5   6   7   8   9   10  11  12  13
  125.  *
  126.  * (last leaf)  87
  127.  * heapify      18
  128.  *                  22
  129.  *                          34
  130.  *                                          87
  131.  *
  132.  * Tracing Table:
  133.  *
  134.  * k    child   items(child)    items(child+1)
  135.  *---   -----   ------------    --------------
  136.  * 1    2           18              24
  137.  * 2    4           22              38
  138.  * 4    8           34              97
  139.  * 8    (none) <-- put 'last' here
  140.  *
  141.  *****************************************************************************/
Advertisement
Add Comment
Please, Sign In to add comment