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 two code segments:

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

    As you will observe, the two code segments 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]
    2. Evaluating the micro-analysis of the two code segments for large values of n, would you expect that execution of Segment 1 would take longer or shorter than Segment 2, or would the 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 the two 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, there is no need to 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-alt.c in your directory for this lab.

    1. Compile and run the program with the values 1, 7, 3, 5, 4, 2, 9, 8, 2, 6. Does the program produce the correct output
    2. Now run the program with a few of your own values. Does the program still produce correct output?
      Hint: Try making some elements in the list negative, after some positives.
    3. 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.
    4. 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
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.