CS 415, Section 001 Sonoma State University Fall, 2022
 
Algorithm Analysis
Instructor: Henry M. Walker

Lecturer, Sonoma State University
Professor Emeritus of Computer Science and Mathematics, Grinnell College

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.

Worksheet/Lab on Comparing Sorting Algorithms and on the Use of Algebra to Improve Efficiency

This assignment has two parts:

Comparison of Sorting Algorithms

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:

With those notes, details of this part of the lab-based exercise follow:

  1. 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.

  2. Review the sort-comparisons.c program to answer the following:

    1. What is the purpose of the struct sorts structure? How is this type used in the program
    2. What is the purpose of the arrays, algAscActive, algRanActive and algDesActive?
    3. When the program is run, sometimes the timing for an algorithm on a data set is given by a number (in seconds) and sometimes as ---. How does the program decide when each of these results will be printed?
    4. The code copies data sets from asc, ran, dex to tempAsc, tempRan, tempDes for each algorithm. Why is this copying necessary?
    5. What is the purpose of the functions 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?
  3. 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:

    1. For θ(n2) sorts, one would expect processing time to increase by a factor of 4 when the size of the data set doubles. Explain why. Although experimental data may show some variation, review the timings obtained and identify which sorts show this increase in processing times.
    2. For θ(n) sorts, one would expect processing times to double when the size of the data set doubles. Explain why, and discuss which, if any, sorts show this increase in processing times.
    3. For θ(n log n) sorts, one would expect processing times to increase a little more than double when the data set doubles in size. Explain why, and discuss which, if any, sorts show this increase in processing times.

Using Algebra to Improve Efficiency

Horner's Rule

  1. 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:

  2. Refer to Levitin, Section 6.5, to complete the following when a = 3.

    1. Apply the left-to-right binary exponentiation algorithm to compute a13.
    2. Apply the right-to-left binary exponentiation algorithm to compute a13.

    Notes: For each part of this problem: