CS 415, Section 001 | Sonoma State University | Fall, 2022 |
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.
Many Web sites provide summation formulas for sums of the form:
for various constants a (e.g., a = 1, 2, 3, ...).
Once such reference is "Summation formulas" from Tripod
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++;
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.)
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.
Determine if the resulting code has Θ(n), Θ(n2), Θ(n3), Θ(n4), and Θ(n5). In each case, give a careful argument justifying your conclusions.
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.
Review the recurrence examples from Appendix B in the text, and study the discussion of the Master Theorem that begins in page 490.
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-alt.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 i
is returned for any i
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 |
|
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu. |
Copyright © 2011-2022
by Henry M. Walker.
|