0

Looking for some simple tips to fix my heapSort. Hopefully it is something simple that I am not comprehending.

I am having a problem with my dumpHeap() and extractMax() methods. It seems to put the dumpheap in order but adds a 0 to my array. It still is technically a heap but bothers me why a 0 appears.

My other question is that my extractMax will not extract the max numbers in descending order which list the numbers that were extracted.

My outputs read which are wrong:

Original Array : 10 2 8 4 18 20 3 16 5

Max-Heap : 20 18 16 10 8 4 2 3 0 5

Extract Max : 20 5 0 3 2 4 8 10 16

If I could get some tips to where to look/change things around, it would be greatly appreciated.

Thanks!

public class heap {

   public int size;
   public int [] H;
   public int position;

   public heap(int size)
   {
           this.size=size;
           H = new int [10];
           position = 0;
   }

   public void createHeap(int [] arrA)
   {
           if(arrA.length>0)
           {
                   for(int i=0;i<arrA.length;i++)
                   {
                          insert(arrA[i]);

                   }
           }        
    }

   public void dumpHeap()
   {
           for(int i=0;i<H.length;i++)
           {
                   System.out.print(" " + H[i]);            
           }
           System.out.println("");
   }

   public void insert(int x)
   {
           if(position==0)
           {
                      H[position]=x;
                      position = 2;
           }else{
                      H[position++]=x;
                      exchange();
                }
   }

  public void exchange()
  {
            int pos = position-1;
            while(pos > 0 && H[pos] > H[pos/2])
            {
                   int y = H[pos];
                   H[pos]=H[pos/2];
                   H[pos/2] = y;
                   pos = pos/2;
            }
  }

  public int extractMax()
  {
          int max = H[0];
          H[0]=H[position-1];
          H[position-1]= 0;
          position--;   
          extractSort(0);
          return max;
  }

  public void extractSort(int k)
  {
           int a = H[k];
           int maxNum =k;
           if(2*k>position && H[maxNum]<H[2*k])
           {
                    maxNum = 2*k;
           }

           if(2*k+1>position && H[maxNum]<H[2*k+1])
           {
                   maxNum = 2*k+1;
           }

           if(maxNum!=k)
           {
                      swap(maxNum,k);
                      extractSort(k);
           }

    }

    public void swap(int a, int b)
    {

             int temp = H[a];
             H[a] = H[b];
             H[b] = temp;
    }
}

public class heapMain {


     public static void main(String args[])
    {
           int arrA [] = {10,2,8,4,18,20,3,16,5};
           System.out.print("Original Array : ");
           for(int i=0;i<arrA.length;i++)
           {
                  System.out.print("  " + arrA[i]);
           }

           heap h = new heap(arrA.length);
           System.out.print("\nMax-Heap : ");
           h.createHeap(arrA);      
           h.dumpHeap();

           System.out.print("Extract Max :");
           for(int i=0;i<arrA.length;i++)
           {
                     System.out.print("  " + h.extractMax());
           }
     }
 }
Eagles11
  • 31
  • 8
  • 1
    Have you traced through your code in your IDE debugger? That is the place to start. You should do that first and then when you find something that is not behaving the way you expect, ask a specific, detailed question. – Jim Garrison Mar 16 '16 at 05:52
  • 1
    I did trace it through the IDE debugger and found nothing which made me believe that I was using the wrong wording and I have edited my question. Thank you. My specific questions are within my dumpHeap() and extractMax() methods and the output I am getting. – Eagles11 Mar 16 '16 at 06:12

1 Answers1

0

I think you messed up with the index of the array.

According to you insert function, you skipped index 1 and set position to 2, which will left H[1] a 0, and that's why you see a 0 at dumpHeap().

If you are trying to implement a heap like here, your parent index selection algorithm in exchange() has some problem. The parent of index 1 is index 0, while the parent of index 2 and 3 is index 1. This algorithm may be fine if you skip index 0 in all of your methods, but it seems you mix up with them.

1st, don't skip index 1 at insert()

public void insert(int x)
{
    H[position++] = x;
    exchange();
}

2nd, calculate your parent and child index correctly.

public void exchange()
{
  int pos = position-1;
  int parentPos = (pos-1)/2;
  while(pos > 0 && H[pos] > H[parentPos])
  {
     int y = H[pos];
     H[pos]=H[parentPos];
     H[parentPos] = y;
     pos = parentPos;
     parentPos = (pos-1)/2;
  }
}

3rd, use correct condition in extractSort()

  public void extractSort(int k)
  {
       int baseValue = H[k];
       int maxNumPos =k;
       int lhsPos = 2*k+1;
       int rhsPos = 2*k+2;
       if(lhsPos < position && H[maxNumPos] < H[lhsPos])
       {
           maxNumPos = lhsPos;
       }
       if(rhsPos < position && H[maxNumPos] < H[rhsPos])
       {
           maxNumPos = rhsPos;
       }

       if(maxNumPos!=k)
       {
                  swap(maxNumPos,k);
                  extractSort(maxNumPos);
       }
  }

The output may be what you want.

Original Array : 10 2 8 4 18 20 3 16 5

Max-Heap : 20 16 18 10 4 8 3 2 5

Extract Max : 20 18 16 10 8 5 4 3 2

Community
  • 1
  • 1
Nier
  • 638
  • 7
  • 15
  • That helped me out a lot, It is totally clear now that I was mixing my indexes. Thank you! Another Idea I wanted to try if I may ask is, I want to change my dumpHeap() method to print out a tree like structure instead of the arrays. What would I need to have in my method to print whats in the array? – Eagles11 Mar 16 '16 at 14:02
  • What do you mean **tree-like structure**? Do you mean you want the output looks like [this](http://i.imgur.com/tNqUy0Z.png)? Actually that array is already a heap structure, just represented as an array. You just need to play with the output algorithm. – Nier Mar 16 '16 at 14:46
  • yes that is exactly how I wanted the output to be like. I just wasn't sure if I have to use the locations lhsPos and rhsPos variables or if I could just use the position of the arrays. – Eagles11 Mar 16 '16 at 15:49
  • All data is already store in that array. You just need to extract what you want to the output. For example if you want to output what I linked before, you can use [this code](http://ideone.com/PwyiCH). I won't explain too much here because it's off the topic. – Nier Mar 16 '16 at 17:21