'building a bottomUp heap

im trying to do a heap bottom up construction from Psuedo code from my text book however the output im getting is not a correct heap im getting out 2 9 8 6 5 7

anyone know where im going wrong(pseudo code is from a text book, and heap needs to be array)

here is the PsuedoCode bottom up im working with

//constructs a heap from elements of a given array by bottom up algorithm
//input an array H[1...n] of orderable items
//output a heap H[1...n]

for i<- [n/2] downto 1 do
 k<-i; v<-H[k];
 heap<-False
  while not heap and 2*k <= n do
   j<-2*k
   if(j<n) //there are two children
    if H[j] < H[j+1] j<- j+1
   if v>=h[j]
    heap = true
   else H[k]<-H[j]  k<-j
 H[k] <-V

here is my code

package heapbottomup;

import javax.swing.Spring;

public class heapbottomup {
public static void main(String args[]){
    int[] array = {2 ,9 ,7 ,6 ,5 ,8};
    BottomUp(array);
}

static int[] BottomUp (int[]array){
    int n = array.length-1;

    for(int i=(n/2);i>=1;i--){
        int k =i;
        int v = array[k];
        boolean Heap = false;

        while(!Heap && ((2*k)<=n)){
            int j = 2*k;
            if (j<n){
                if(array[j]<array[j+1]) j =j+1;
            }
            if(v>=array[j]){
                Heap=true;
            }
            else{
                array[k]= array[j];
                k=j;
            }
            array[k]=v;
        }//end while

    }//end for
    print(array);
    return(array);
}


static void print(int[]array){
    if(array==null){
        System.out.println("empty");
        return;
    }
    for(int i =0;i<array.length;i++){
        System.out.print(array[i] + " ");
    }
    System.out.println();
}//end print


}


Solution 1:[1]

Arrays in Java start at index 0 and go to n-1 (not 1 to n as in the pseudo-code). You correctly initialize n to be array.length-1, but then you should also make the for-loop go while i >= 0 not i >= 1. I didn't run your program to see if there are other problems, but that seems like the first thing to fix.

Solution 2:[2]

The key points are:

  • In order to hold the property of a node at position j that its left child is at 2j, its right child is at 2j +1 and its parent is at j/2, the range of j is: 1<=j<=n.
  • The array range is from 0 to n-1

The trick is: In the for and while loops, you still think of the range 1 to n, but when you do the array manipulation, you only need to offset 1, ie, array[j-1] for the position j.

Try with the following code, I tested and it worked - the output is 9 6 8 2 5 7. Please be aware that array[k-1]=v isn't in the while loop.

public class heapbottomup {
    public static void main(String args[]){
        int[] array = {2 ,9 ,7 ,6 ,5 ,8};
        BottomUp(array);
    }

    static int[] BottomUp (int[]array){
        int n = array.length;

        for(int i=(n/2);i>=1;i--){
            int k =i;
            int v = array[k-1];
            boolean Heap = false;

            while(!Heap && ((2*k)<=n)){ 
                int j = 2*k;
                if (j<n){
                    if(array[j-1]<array[j]) j =j+1;
                }
                if(v>=array[j-1]){
                    Heap=true;
                }
                else{
                    array[k-1]= array[j-1];  
                    k=j; 
                }                
            }//end while

            array[k-1]=v; 

        }//end for
        print(array);
        return(array);
    }

    static void print(int[]array){
        if(array==null){
            System.out.println("empty");
            return;
        }
        for(int i =0;i<array.length;i++){
            System.out.print(array[i] + " ");
        }
        System.out.println();
    }//end print
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Always Learning
Solution 2