Homework 9 - Heapsort (Extra Credit)
Due: Wednesday June 13, 2012 at midnight
This is an extra credit assignment. No late submissions will be accepted. The
code for this assignment will not be on the final, BUT the *concepts* may be
on the final (e.g. you should be able to draw a min-heap or max-heap and show
insertions and deletions).
The purpose of this assignment is to implement an array-based heap data
structure and the associated heapsort algorithms. This assignment specifically
implements a max-heap.
For this assignment, you will be making an array-based heap structure that
will contain an array of elements, the count of elements in the array and the
capacity of the array. The array should be a dynamic array so that it can be
adjusted to fit arrays of different sizes. So the capacity will reflect how
many elements were allocated to the array by the new command. This data
structure will be passed to (or created by) the various algorithms associated
with heaps and heapsort.
You will define the following functions:
- Heap *createHeap(int size) - Create a new, empty heap with "size" elements
in the array and the count initialized to 0. Don't forget to set the
capacity to size.
- void deallocateHeap(Heap *) - Deallocates the array allocated to the heap
by createHeap and then deallocates the heap itself.
- bool empty(Heap *h) - Return true if the heap is empty, false otherwise.
- bool full(Heap *h) - Return true if the heap is full (count == capacity),
false otherwise.
- void percolateDown(Heap *h, int position) - Apply the percolate down method
to the heap element at the indicated position. If the position is an invalid
index, return out of the function without affecting the heap.
- void percolateUp(Heap *h, int position) - Apply the percolate up method to
the heap element at the indicated position. If the position is an invalid
index, return out the function without affecting the heap.
- void heapify(Heap *h, ELEMTYPE *array, int size) - Create a heap to store
the given array by calling createHeap. Copy the array values to the heap.
Apply the heapify algorithm to turn the array into a valid max-heap.
- void heapsort(ELEMTYPE *array, int size) - Using heapify, create a local
heap out of the given array and perform the in-array based heapsort
algorithm on the resulting heap. This will result in the heap containing
the sorted values of the array. Copy these values back over to the passed
array to update it to the sorted array. Return out of the function without
doing anything if the size is invalid (less than 2). Don't forget to call
deallocateHeap to delete the local heap before exiting this function.
Your main function will take two filenames from the command line (similar to
the mergesort assignment). The first filename will be the input file containing
the original array. The second filename will be the output file for outputting
the sorted array. Read the data from the file into an array local to main. Call
the heapsort function, passing it the array. After heapsort returns, output
each element of the array (which should be sorted) to the output file.
Email the source code to me. If you cannot finish the assignment by the due
date, email me what you have completed for partial credit.