I am trying to create a binary heap with arrays. I have succesfully made the heap with buildHeap
and heapify
. My problem is when I try to insert
a new element into the array, and when I try to sort it with heapSort
.
Here is what I have for the heapify
function:
void heap::Heapify(int arr[], int i){
int largest = i;
int L = LeftChild(i);
int R = RightChild(i);
if (L <= heapSize && arr[L] > arr[i])
largest = L;
else{
largest = i;
}
if (R <= heapSize && arr[R] > arr[largest]){
largest = R;
}
if(largest != i){
int temp = arr[i];
arr[i] = arr[largest];
arr[largest] = temp;
Heapify(arr, largest);
}
}
And here is my buildHeap
function:
void heap::BuildHeap(int arr[]){
for(int i = (heapSize/2)-1; i >= 0; i--){
Heapify(arr, i);
}
}
These two functions work, while the following are not working, for the insert it is not inserting it in the proper location.
Here is the insert
function:
void heap::Insert(int arr[], int key){
heapSize++;
int i = heapSize - 1;
while(i > 0 && arr[Parent(i)] <= key){
arr[i] = arr[Parent(i)];
i = Parent(i);
}
arr[i] = key;
}
With the heapSort
function it is sorting it for the most part but will print it like so (the first line is the heap before it is sorted):
32 24 5 19 23 4 3 11 2 12
5 2 4 3 23 19 32 11 24 12
and here is my heapSort
function:
void heap::HeapSort(int arr[]){
for(int i = heapSize - 1; i >= 0; i--){
int temp = arr[0];
arr[0] = arr[i];
arr[i] = temp;
heapSize = heapSize - 1;
Heapify(arr, 0);
}
}
Any help to figure how these two functions are not working properly would be greatly appreciated.