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.
As suggested by this assignment's title, this exercise is organized into three parts:
Consider the following directed three graphs.
Directed Graph 1 | Directed Graph 2 | Directed Graph 3 |
For each of these graphs, answer the following:
The Reading on Quicksort discusses four possible loop invariants for the Partition Problem, each based on using the left element in an array segment as a pivot. A similar, but alternative, approach uses the right element in an array segment as a pivot. This alternative considers three additional loop invariants.
Based on the Reading on Quicksort, the program partitions-alt.c provides
Review the program and explain
typedef struct algs { char * name; int (*proc) (int [ ], int, int, int); } partitionType;and how this structure is used.
the purpose of the line
#define printCopyTime 0 // 1 = print times to copy arrays; 0 = omit this outputand how
printCopyTime
is used.
what steps are involved to time segments of code in C/C++ (be sure to identify what function(s) is(are) called).
explain the purpose of variables, maxreps
and copy_time
, and why these variables are
needed.
Expand this program by inserting the following two functions and updating the main procedure to include them in test runs:
Note: Both of these procedures must be based on the Loop Invariants specified. Procedures violating the specified Loop Invariant will lose [almost] all credit for that part.
After running the expanded program from Step 2, turn in a copy of the output obtained, and answer these questions.
Finding the kth smallest item: The partition method may be used to find the kth smallest element in an array, by narrowing the range to be examined within the overall array. For example, suppose that partition returns index middle as the location of the final location for the pivot. Basic processing involves three cases:
If middle is k-1, that is, if middle is the kth element in the array, then the element at that position is the kth smallest.
If middle < k-1, then one should look for the k smallest element in the subarray to the left of the index middle.
If middle > k-1, then one should look for the k smallest element in the subarray to the right of the index middle.
Write a procedure kthSmallest to find the kth smallest element in any array. kthSmallest should use procedure partition and the above notes the above algorithm to guide its processing. Your lab write up should include the code for kthSmallest, the enclosing program used for testing, and the test runs used for checking correctness.
Note: For an array of size n, setting k to n/2
enables kthSmallest
to find the median value.
Notes on Steps 2 and 3: For Steps 2 and 3 in this assignment, you should submit:
Program quicksort-comparisons.c contains two copies of quicksort procedures and a framework for timing the running of these procedures on ascending, random, and descending data sets of varying sizes. In particular,
basicQuicksort, basicQuicksortHelper
,
and basicPartition
implement the basic quicksort algorithm.
imprQuicksort, imprQuicksortHelper
,
and imprPartition
begin as copies of the
corresponding basic functions, and will be modified as part of
this problem.
These functions come directly from the Reading on Quicksort
left
and right
, and swapping
that element with the element at array
index left
. Otherwise, the "improved quicksort"
is the same as the "basic" version.
Data Set Times Algorithm Size Ascending Order Random Order Descending Order basic quicksort 40000 1.1 ok 0.0 ok 1.1 ok improved quicksort 40000 1.1 ok 0.0 ok 1.1 ok . . . basic quicksort 160000 18.1 ok 0.0 ok 18.0 ok improved quicksort 160000 17.9 ok 0.0 ok 18.0 ok basic quicksort 320000 ---- 0.0 ok ---- improved quicksort 320000 0.0 ok 0.0 ok 0.0 ok . . . basic quicksort 2560000 ---- 0.4 ok ---- improved quicksort 2560000 0.2 ok 0.4 ok 0.2 ok . . .
Expand the program in Step 4 to include a hybrid quicksort function (with any needed helper functions—perhaps copied with minor revision from the improved quicksort). The hybrid quicksort, is described in the Reading on Quicksort.
Notes on Steps 4 and 5: Since Step 5 extends Step 4,
created August 6, 2022 revised August 9, 2022 revised September 27, 2022 revised December 30, 2022 revised Summer, 2023 revised Novemver 30, 2024 |
|
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu. |