Question
Implement the Heap Sort algorithm. To accomplish this, you will need to implement a BinaryHeap class. This class will be templated. All constructors will need
Implement the Heap Sort algorithm. To accomplish this, you will need to implement a BinaryHeap class. This class will be templated. All constructors will need to take a pointer to a comparison function. One constructor will also have to take a pointer a dynamically created array and the size of that array as parameters. This constructor will heapify this array (more on that below). Constructors that don't take the array pointer and size will have to dynamically create an array to store heap items. The class will have methods for inserting an item into the heap, removing an item from the heap, getting the number of items in the heap, and peeking at the top item of the heap. The Heapify algorithm assumes that the items to be inserted in the heap already exist in the array that was passed in to the constructor. For each item in the array, it will be added to the heap. Remember that in a binary heap, items are added to the queue portion of the heap (i.e. added to the end of the existing heap elements), then added to the tree, then finally shifted along the path towards the root until it sits in the appropriate position. Using an array for this means that adding an item to the heap is as simple as inserting the M+1 element of the array to the heap (where M is the index of the last element of the heap). Once you have a binary heap working as outlined above, use it to sort the array. Remember that removing items from the binary heap always removes from the root of the tree (swapping it with the last element in the heap queue). Implement a driver program to demonstrate the heap sort using integers. (c++)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started