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.
As suggested by this worksheet/lab's title, this exercise is organized into three parts:
Consider the following directed graph.
(not required, but available for extra credit) Describe the efficiency (Big-θ) of the topological sort algorithm for a directed graph with v vertices and e edges, assuming edge information is stored in an adjacency list, and justify your answer.
Based on the Reading on Quicksort, the program partitions.c provides
typedef struct algs { char * name; int (*proc) (int [ ], int, int, int); } partitionType;and how this structure is used.
#define printCopyTime 0 // 1 = print times to copy arrays; 0 = omit this outputand how
printCopyTime
is used.
maxreps
and cop_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:
Run your expanded program from Step 3, and print out the output obtained. Then answer these questions.
swap
function, rather than writing the
three lines of code inline within a partition function?
Finding the kth largest 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 largest element in the subarray to the left of the index middle.
If middle > k-1, then one should look for the k largest element in the subarray to the right of the index middle.
Write a procedure select to find the kth largest element in any array. select should use procedure partition and the above notes the above algorithm to guide its processing. Your lab write up should include the code for select, 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 select
to find the median value.
Program quicksort-comparisons.c contains two copies of quicksort procedures and a framework for timing the runting 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 6 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.
created August 6, 2022 revised August 9, 2022 revised September 27, 2022 |
|
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu. |