I'm not sure if I'm doing this correctly but up to now I get an exception error. Can you see why? :
public void countHeap(){
System.out.println("input data:");
for (int i = 0; i < dataSet.length; i++) {
System.out.print(" "+dataSet[i]);
count++;
}
noDataElements = count;
formHeapTree();
}
public void formHeapTree(){
int unsortedSet[] = new int[noDataElements];
noDataElementsHalf = (noDataElements+1)/2;
System.out.println("\nheap Array");
for (int j = 0; j < unsortedSet.length ; j++) {
for (int i = noDataElementsHalf; i < noDataElements; i++) {
unsortedSet[i] = dataSet[j];
}
System.out.print(" "+ unsortedSet[j]);
}
sortLevel();
for (int j = 0; j < unsortedSet.length ; j++) {
for (int i = 0; i < noDataElementsHalf; i++) {
unsortedSet[i] = dataSet[j];
}
System.out.print(" "+ unsortedSet[j]);
}
}
public void sortLevel(){
System.out.print("\nheap array \n ");
for (int i = unsortedSet.length; i > 0; i--) {
int j = i-1;
//Parent must be smaller than child
if(unsortedSet[j] > unsortedSet[i]) { int temp = unsortedSet[i];
unsortedSet[i] = unsortedSet[j];
unsortedSet[j] = temp;
}
}
}