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:  Analysis of Recursive and Non-recursive Algorithms, Insertion Sort, and Assertions

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

Reference

Many Web sites provide summation formulas for sums of the form:

1a + 2a + 3a + ... + na

for various constants a (e.g., a = 1, 2, 3, ...).

Once such reference is "Summation formulas" from Tripod

Algorithmic Analysis

  1. Consider the following code segment:

        int sum = 0;
    
        for (int i=1 ; i <= n * n; i+= n)
          for (int j=1 ; j <= 10 ; j++)
            for (int k=0; k < i ; k++ )
                sum++;
    
    1. Develop an algebraic expression for the function f(n) that describes the time required for processing, given n. (If the time depends upon specific details of input, consider a "worst-case" analysis.)

    2. Determine if the resulting code has O(n), O(n2), O(n3), O(n4), and O(n5). In each case, give a careful argument justifying your conclusions.

    3. Determine if the resulting code has Θ(n), Θ(n2), Θ(n3), Θ(n4), and Θ(n5). In each case, give a careful argument justifying your conclusions.

    4. Naturally, the running time of this code segment will depend upon the particular computer and compiler used. On a particular machine and compiler, suppose the running time for this code is 8 seconds when n is 1000. Based on your answers earlier in this problem, estimate the likely time for the running of this code when n is 2000 and when n is 5000. Justify your answer briefly.

    Notes

  2. Review the recurrence examples from Appendix B in the text, and study the discussion of the Master Theorem that begins in page 490.

    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 .

    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. 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?
    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.
    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]
    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
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.