CS 415, Section 001 | Sonoma State University | Fall, 2022 |
Algorithm Analysis
|
||
Instructor: Henry M. Walker
Lecturer, Sonoma State University |
Although much of this course is well developed, some details can be
expected to evolve as the semester progresses.
Any changes in course details will be announced promptly during class.
This assignment has two parts:
This part of this worksheet/lab asks you compare the performance of five sorting algorithms, including:
To experimentally determine the efficiency of these algorithms, this exercises asks you to use program sort-comparisons.c. (Thus, coding must be in C/C++.) This program provides a framework in which each algorithm can be timed on several data sets in ascending, random, and descending order. Several details for this exercise follow:
sort-comparisons.c provides code for the Straight Selection Sort and Insertion Sort, taken from class discussions and slides.
You will need to add code for the Hybrid Quicksort, Merge Sort, and Heap Sort, based on the readings from the textbook and in-class discussion.
The code for the Hybrid Quicksort should utilize the best sorting option from the Worksheet/Lab on Quicksort, Partition, and Topological Sorting. Thus, your code likely should include a partition function in which an inner loop scans down to find a small item, then scans up to find a large item, and then swaps the small and large items. Similarly, the sorting algorithm should switch from a quicksort to an insertion sort for small array segments (perhaps segments of 9 or less). If your previous lab indicated some other version of quicksort was faster, contact the instructor for further guidance.
The code for the Merge Sort must follow the outline given in the Reading on Merge Sort. Note that many implementations of a merge sort devote considerable time and effort to allocating (and deallocating) temporary arrays. However, the entire algorithm can be accomplished with only one auxiliary array of the same size as the original (and no other arrays). Further, for most steps of the algorithm, there is no need to copy data twice—first to a new array and then back to the original. For this sort, any correct code for a proper merge sort will obtain at least half credit for the merge sort, but code with much memory allocation/deallocation or excessive copying of data between arrays may lose up to 50% of the merge-sort points.
The Heap Sort draws upon the heap data structure, introduced in the Workshop/Lab on ... Heaps. The key procedure has the following specification:
/** ******************************************************************************* * percDown function * * @param array the array to be made into a heap, starting at hold * * @param hole: base of subtree for start of processing * * @param size the size of the array * * @pre all nodes in left and right subtrees of the hole node are heaps * * @post all nodes in the tree from the hole node downward form a hea * *********************************************************************************/ void percDown(int array [ ], int hole, int size)With this procedure, a Heap Sort uses the following code:
** ******************************************************************************* * heap sort, main function * * @param a the array to be sorted * * @param n the size of the array * * @post the first n elements of a are sorted in non-descending order * *********************************************************************************/ void heapSort (int a [ ], int n) { // Build Heap for (int i=n/2 ; i>=0 ; i-- ) { percDown(a, i, n); } for (int i=n-1 ; i>0 ; i-- ) { int tmp = a[0]; a[0] = a[i]; a[i] = tmp ; // deleteMax percDown( a, 0, i); // Maintain heap ordering property } }
Additional details may be found in Levitin's text, Section 6.4.
Since an important point of this exercise is to compare sorting algorithms, you should try to code each algorithm efficiently in a similar style.
With those notes, details of this part of the lab-based exercise follow:
Complete the Hybrid Quicksort, Merge Sort, and Heap Sort in sort-comparisons.c as described above. You may NOT change the main procedure in any way, unless you have explicit permission from the instructor. Of course, your code must follow the CS 415 C/C++ Style Guide for Fall 2022.
Review the sort-comparisons.c program to answer the following:
struct sorts
structure? How is
this type used in the program
algAscActive, algRanActive
and algDesActive
?
---
. How does the program decide when
each of these results will be printed?
asc, ran, dex
to tempAsc, tempRan, tempDes
for each algorithm.
Why is this copying necessary?
checkAscValues
and checkAscending
? Explain how these functions
help automate checking that the algorithms are properly coded.
Also, explain why both procedures are needed—why not just
use one of these procedures for checking?
Once the program runs properly, print a copy of the results (you can copy and paste from a terminal window to an editor that uses a fixed-width font). Then analyze the results to answer these questions:
A polynomial function has the form
p(x) = anxn + an-1xn-1 + ... + a2x2 + a1x + a0
Write a function compute_poly that takes three parameters:
and returns the value of the polynomial p(x).
Notes:
Refer to Levitin, Section 6.5, to complete the following when a = 3.
Notes: For each part of this problem: