CS 415, Section 001 Sonoma State University Spring, 2023
 
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.

Assignment 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. 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, asc, ran and des?
    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?
  2. 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.

    Note: A listing of the complete program must be included in the work submitted for this assignment.

  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:

created July 31, 2022
revised December 31, 2022–January 3, 2023
typo fixed March 23, 2023
Valid HTML 4.01! Valid CSS!
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu.
ccbyncsa.png

Copyright © 2011-2022 by Henry M. Walker.
Selected materials copyright by Marge Coahran, Samuel A. Rebelsky, John David Stone, and Henry Walker and used by permission.
This page and other materials developed for this course are under development.
This and all laboratory exercises for this course are licensed under a Creative Commons Attribution-NonCommercial-Share Alike 4.0 International License.