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.

Worksheet:  Analysis of Recursive and Non-recursive Algorithms, Insertion Sort, and Assertions

This worksheet is organized into three parts: algorithmic analysis, insertion sort, and assertions.

As with past assigmnets, feel free to consult outside sources to simplify summation formulae. One such reference is Common Computational Formulae from the course's home page.

Algorithmic Analysis

  1. Consider the following three code segments:

    //Segment 1                     //Segment 2                      //Segment 3
    int sum = 0;                    int sum = 0;                     int sum = 0;
    for (int i=1; i<=n; i*=2)       for (int i=1; i<=n; i*=2)        for (int i=1; i<=n; i++)
       sum++;                          for (int j=1; j<=i; j++)         for (int j=1; j<=i; j *= 2)
                                          sum++;                           sum++;
    

    As you will observe, the loop for Segment 1 is identical with the outer loop in Segment 2, and Segments 2 and 3 are identical, except that the updating of variables i and j is swapped.

    1. As in previous assignments, perform a careful micro-analysis of the time required for each of these code segments, using the following constants for the time it takes a machine to do specific operations:

      • A — one assignment
      • B — one evaluation of a Boolean (e.g., a comparison)
      • S — one addition or subtraction
      • M — one multiplication or division
      • P — one increment (e.g., ++) [since the line w++ is largely equal to x = x + 1, P is roughly equal to A + S]

      Note: If it would be useful in your computations, for this problem you may assume n is a power of 2 (i.e., n = 2k or k = log2 n for some integer k).

    2. When considering the micro-analysis Code Segments 2 and 3 for large values of n, would you expect that execution of Segment 2 would take longer or shorter than Segment 3, or would these two code segments run in about the same time? Explain briefly.

    3. Based on your micro-analysis, determine the Big-Θ run-time for each segment. Is the Big-Θ run-time the same for any these segments? Explain briefly.

    4. Use your micro-analysis and the definition of Big-O to determine if the run-time for each code has O(n), O(n2), O(n3), O(n4), and O(n5). In each case, give a careful argument justifying your conclusions.

  2. Review the recurrence examples from Appendix B in the text, and study the discussion of the Master Theorem that begins in page 490. Then, omitting any examples found in the textbook and also omitting any examples discussed in class,

    1. Find two examples that illustrate Case 1 of the Master Theorem .
    2. Find two examples that illustrate Case 2 of the Master Theorem .
    3. Find two examples that illustrate Case 3 of the Master Theorem .

    For each example, for a recurrence relation involving


    Note: In this problem, you should NOT perform any of the computations that were used to prove the Master Theorem. Similarly, you do not need to use backward or forward substitution in obtaining an answer. Just use the hypotheses and conclusions of the Master Theorem itself!

Insertion Sort

  1. Today's reading discusses the insertion sort algorithm for ordering data within an array. Write one to three sentences to answer each of the following questions.

    1. The reading claims that insertion sort is better for nearly-sorted lists than reverse-ordered lists. Why?
    2. How much extra memory is required for each sort? That is, beyond the original array and an integer variable giving the size of the array, what other variables are needed and what type should those variables be?
    3. If the list is composed of four identical elements, how many elements change position when insertion sort is used? Why?
    4. Why might some people use insertion sort in real life?
  2. Download and save the program insertion-sort-alt2.c in your directory for this lab.

    1. Compile and run the program with a few of your own array values. Does the program produce correct output?
      What patterns, if any, do you identify in the output?
    2. Read through the program to locate the source of the error and fix it.
      Hint: the error is caused by one line in the program.
    3. Write a paragraph explaining why this error caused the output you saw.

Assertions

  1. Consider a binary search for an item in an integer array a of length n. Thus, the header for this function should be

         int binSearch (int n, int a [ ], int item)
       

    Informally, the results of the search should be as follows:

    1. Give a careful statement of all necessary pre- and post-conditions for this function—likely using notation or statements similar to formal logic.
    2. For each pre-condition, either write code that might appear at the start of the function to test whether the condition is true, or explain why such a condition cannot be checked in C/C++.
    3. For each post-condition, explain how the condition could be tested within the function (e.g., just before a return statement), or explain why the condition cannot be tested.
  2. Consider the Insertion Sort algorithm from the Reading on Insertion Sort.

    // method to sort using the insertion sort
    // parameters:  a, the array to be sorted
    //              length, the size of the a array
    void insertionSort (int a [], int length) {
       for (int k = 1; k < length; k++) {
          int item = a[k];
          int i = k-1;
          while ((i >= 0) && a[i] > item){
             a[i+1] = a[i];
             i--;
          }
          a[i+1] = item;
       }
    }
        
    1. Give a careful statement of all necessary pre- and post-conditions for this procedure—likely using notation or statements similar to formal logic.
      • Note that data type compatibility is checked by a compiler, so explicit pre-conditions for such data types likely are redundant and need not be stated.
      • Pre-conditions should require that an insertion sort makes sense and that the code is assured not to crash.
        • Note: the code may crash if the array is [1, 2, 3, 4] when length is 6.
      • Post-conditions should rule out the following cases:
        • [1, 2, 2, 3] is not a sorted version of [1, 1, 2, 3]
        • [1, 1, 1, 1] is not a sorted version of either [1, 1, 1] or [1, 1, 1, 1, 1]
        • [2, 1, 3, 4] is not a sorted version of [4, 2, 4, 1]
        • [1, 2, 3, 4] is not a sorted version of [3, 5, 7, 9]
    2. For each pre-condition, either write code that tests whether the condition is true, or explain why such a condition cannot be checked in C/C++.
    3. For each post-condition, explain how the condition could be tested. Also explain whether testing the post-condition could be added to the code directly with the variables given, or whether additional variables would be needed.
created December, 2021
revised December-January 2021
expanded to include insertion sort and assertions August 1, 2022
revised January 2023
revised Summer 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.