Sonoma State University
 
Algorithm Analysis
Instructor: Henry M. Walker

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

Although CS 415 has been well developed for several years, last year the CS faculty made a significant, long-term curricular change regarding SSU's Upper Division GE Area B Requirement.

Assignment on Recursive and Non-recursive Algorithms, Insertion Sort, and Assertions

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

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

Algorithmic Analysis

  1. A pre-condition for each of the following three code segments states that n is an integer power of 2 (that is n = 2k for some integer k).

    //Segment 1                       //Segment 2                         //Segment 3
    int sum = 0;                   int sum = 0;                        int sum = 0;
    for (int i=1; i<=n*n*n; i++)   for (int i=1; i<=n*n*n; i++)        for (int i=1; i<=n*n; i++)
       sum++;                         for (int j=1; j<=i*i; j++)          for (int j=1; j<=i*i*i; j ++)
                                         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 bounds/updating of variables i and j is swapped.

    1. As in previous assignments, perform a careful micro-analysis of the time required for each line for each of these code segments, without using any common computational formulae. In this analysis, use 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. For each code segment, add the times to obtain the total time.

    3. For each code segment, use Common Computational Formulae as needed to obtain a reasonably compact expression for the total run time.

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

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

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


    Notes:

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. 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?
    2. If the list is composed of four identical elements, how many elements change position when the insertion sort is used? Why?
    3. If the list is already sorted in non-descending order, how many times do elements change position when the insertion sort is used? Why?
    4. If the list is sorted in descending order, how many times are elements moved (by one or more positions) when the insertion sort is used? Why? (For example, if an element is moved once at one point in the algorithm and then moved again later on, the element would be considered as having moved twice.)
    5. Why might some people use insertion sort in real life?
  2. Download and save the program insertion-sort-alt3.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 errors and fix them.
      Hint: the difficulties are caused by 1-3 characters in each of two lines in the program.
    3. Write a paragraph explaining why these errors 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.

    Notes: Post-conditions should address the following circumstances.

  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 [3, 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
revised November 2024
Valid HTML 4.01! Valid CSS!
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu.
ccbyncsa.png

Copyright © 2011-2025 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.