Version 10/01/17
Educational Objectives: After completing this assignment, the student should be able to accomplish the following:
Background Knowledge Required: Be sure that you have mastered the
material in these chapters before beginning the assignment:
Iterators,
Generic Algorithms,
Introduction to Trees,
Binary Heaps, and
Advanced Heap Algorithms.
Operational Objectives: Implement two new generic heap algorithms fsu::g_heap_repair and fsu::g_build_heap. Move the current implementation of g_heap_sort into the alt namespace and re-implement fsu::g_heap_sort using the new algorithms. Test both versions fsu::g_heap_sort and alt::g_heap_sort using sortspy.cpp and tease out the differences in runtimes, providing details in log.txt.
Deliverables: Three files:
gheap_advanced.h # fsu::g_heap_sort and alt::g_heap_sort, plus other algorithms makefile # builds sortspy.x and hsort.x log.txt # your project work log
The official development/testing/assessment environment is specified in the Course Organizer. Code should compile without warnings or errors.
In order not to confuse the submit system, create and work within a separate subdirectory cop4531/proj2.
Begin by copying the entire contents of the directory LIB/proj2 into your cop4531/proj2/ directory. Then copy the file LIB/tcpp/gheap_basic.h into cop4531/proj2/. At this point you should see these files in your directory:
gheap_basic.h hsort.cpp ranuint.cpp sortspy.cpp deliverables.sh
Copy gheap_basic.h to gheap_advanced.h and edit to satisfy the requirements for that deliverable.
Test your algorithms thoroughly and put your notes and conclusions into your log.txt.
Submit the assignment using the script LIB/scripts/submit.sh.
Warning: Submit scripts do not work on the program and
linprog servers. Use shell.cs.fsu.edu to submit assignments. If you do
not receive the second confirmation with the contents of your assignment, there has
been a malfunction.
Before starting to code, it's a good idea to review one more time the material on Advanced Heap Algorithms. The theory, algorithms, and code are all discussed there. Your delverable gheap_advanced.h should follow the plan outlined in these notes.
Begin coding by moving the current two versions of heap_sort into the namespace alt, keeping only the prototypes in namespace fsu. You will also need to add the namespace fsu:: resolution to the calls to push_heap and pop_heap. The effect is that the old "fsu::g_heap_sort" is now "alt::g_heap_sort".
Add these algorithm prototypes to namespace fsu (including the requisit template statements):
g_build_heap(I beg, I end, P& p); g_build_heap(I beg, I end); g_heap_repair(I beg, I loc, I end, P& p); g_heap_repair(I beg, I loc, I end);
so that the totality of prototypes in the file is:
namespace fsu { g_push_heap (I beg, I end, P& p); g_pop_heap (I beg, I end, P& p); g_heap_sort (I beg, I end, P& p); g_build_heap (I beg, I end, P& p); g_heap_repair (I beg, I loc, I end, P& p); g_push_heap (I beg, I end); g_pop_heap (I beg, I end); g_heap_sort (I beg, I end); g_build_heap (I beg, I end); g_heap_repair (I beg, I loc, I end); } namespace alt { g_heap_sort (I beg, I end, P& p); g_heap_sort (I beg, I end); }
The implementations for the alt versions should also be already there in the file, where you changed the namespace to include them under alt.
Implement all of the namespace fsu algorithms in the namespace fsu. The implementation for fsu::g_push_heap can be the same as it was in the old version. The new algorithms g_heap_repair and g_build_heap obviously require new implementations, using the ideas outlined above in this document. Finally, g_pop_heap and g_heap_sort require the improved implementations, again as outlined above.
Test your various implementations using the supplied sortspy.cpp. Note that this version of sortspy has the additional feature of checking the results of each sort and reporting the number of order errors in the result. Zero is good. Anything else tells you the "sort" algorithm is misnamed.
Once you are sure you have the implementations correct, begin to pay attention to the comp_count data for the two versions of heap_sort. (Keep observations in your log.) Try to tease out a distinction between the two.
By compiling hsort.cpp, you have a test of the default order version of your new heap_sort.
There is an executable you can use to check your comp_count data against what we think is correct: [LIB]/area51/sortspy_i.x.
One optimization you want to use, after all code is debugged and tested, is lowering the cost of the control arithmetic used in the various algorithms. Here multiplying and dividing by 2 are used to calculate the child and parent indices. This integer arithmetic can be made much faster by using the observations:
left = 2*i + 1; // uses integer arithmetic left = (i << 1) | (size_t)0x01; // uses bitwise operations to get same result parent = i/2; // integer arithmetic parent = (i >> 1); // same result using bitwise operations
Because integer arithmetic follows an algorithm quite a few clock cycles may be needed to perform one division or multiplication. The bitwise operations on the other hand have hardware support and may run in as little as one clock cycle.