Sonoma State University | ||
Algorithm Analysis
|
||
Instructor: Henry M. Walker
Lecturer, Sonoma State University |
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.
Historically, CS Majors could satisfy this requirement by taking CS 454, Theory of Computation, and CS 454 will continue in this role for the next several semesters.
At some time in the future (but not Spring 2025), CS 415, Algorithm Analysis, will allow students to satisfy SSU's Upper Division GE Area B Requirement.
During an anticipated time of transition:
For future semesters, students should check with the CS faculty regarding which course(s) (CS 415 and/or CS 454) will satisfy SSU's Upper Division GE Area B Requirement.
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.
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.
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:
w++
is largely equal to x = x + 1
, P is roughly equal to A + S]
For each code segment, add the times to obtain the total time.
For each code segment, use Common Computational Formulae as needed to obtain a reasonably compact expression for the total run time.
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.
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.
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.
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,
For each example, for a recurrence relation involving
Notes:
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.
Download and save the program insertion-sort-alt3.c in your directory for this lab.
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:
item
appears in the array one or more times,
then any i
may be returned where a[i]==item
.
item
does not appear in the array, then -1 is
returned.
return
statement), or explain why the condition
cannot be tested.
Notes: Post-conditions should address the following circumstances.
[10,11,12,13]
and one
is searching for 11
, the binary search should not
return 1, with the new array being [11,11,11,11]
.
[10,11,12,13]
and one
is searching for 14
, the binary search should not
return 4, with the new array being [10,11,12,13,14]
.
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; } }
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 |
|
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu. |
Copyright © 2011-2025
by Henry M. Walker.
|