CS 415, Section 001 | Sonoma State University | Spring, 2023 |
Algorithm Analysis
|
||
Instructor: Henry M. Walker
Lecturer, Sonoma State University |
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.
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.
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.
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:
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).
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
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!
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-alt2.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.
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 |
|
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu. |
Copyright © 2011-2022
by Henry M. Walker.
|