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.
As suggested by this worksheet/lab'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:
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 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:
After running the expanded program from Step 2, turn in a copy of the output obtained, and answer these questions.
swap
function, rather than writing the
three lines of code inline within a partition function?
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 select to find the kth smallest 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.
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 |
|
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu. |