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 assignment is in two parts: Brute Force Algorithms and Merge Sort
This section on contains two exercises related to brute force algorithms.
As discussed in class, a permutation sort uses the following basic algorithm, given an array A of size n:
Various sources indicate that the permutation sort has
O((n-1)!), O(n!), and/or O((n!)*n), where ! is the factorial
operator (so 4! = 4*3*2*1)
Which, if any, of these conclusions is correct? Briefly
justify your answer.
Note: This problem asks for a logical argument and
conclusion, NOT code to perform the permutation sort.
In class, we discussed
program list-processing.c, which
included a recursive printReverseRecursive
procedure that uses recursion to print a linked list in reverse
order. For example, given the list
"Node A" "Node B" "Node C" "Node D" "Node E"the procedure prints
"Node E" "Node D" "Node C" "Node B" "Node A"However, the list is not formatted, with parentheses around the entire list and the list nodes separated by commas, such as
("Node E", "Node D", "Node C", "Node B", "Node A")Program
list-processing.c
contains a stub for a new
procedure printReverseRecursiveFormatted
.
Complete this procedure, so that the resulting code
recursively prints a formatted result of the nodes in reverse
order, as described above.
printReverseRecursive
.
This section compares four implementations of a Merge Sort.
Approach 1: Traditional Recursive Algorithm (with local temporary arrays): The traditional version, such as given in the textbook or at Tutorialspoint, creates two new arrays that are half the size of the original, copies the first and second halves of original array into new arrays, recursively applies the Merge Sort to the two halves, and merges them into the original array.
Approach 2: Traditional Recursive Algorithm (with just 2 working arrays altogether): The traditional version is modified, so that two working arrays are declared at the start (likely in dynamic storage), and these arrays are used for all copying. Thus, new storage need not be declared/allocated with each recursive call, but the arrays are passed as parameters. Otherwise, this algorithm follows the traditional outline.
Approach 3: Iterative Algorithm with Traditional Merge: The recursion of the top-down, traditional algorithm is replaced by an iterative, bottom-up version. This algorithm performs the same merges in largely the same order as the traditional version, effectively starting at the end of the recursive calls and backtracking. In this case, the local arrays are allocated in dynamic memory rather than on the run-time stack. The merge process for merging two lists is identical to the parallel process in the recursive code.
Approach 4: Iterative, with only one auxiliary array (allocated only once):. Suggestions for this approach may be found in the class slides.
Program mergesort-comparisons.c provides a framework for the development of a program to implement and test each of these implementations.
Review program mergesort-comparisons.c.
sortProcs
array.
What information does it hold, how is information used, and
why is it needed?
maxDataSetSize
,
maxRecSize
, and algTimeLimit
, why
are they needed, how are their values set, and how are they
used?
algAscActive,
algRanActive
, and algDesActive
, how are
their values computed, and how are they used?
Run program mergesort-comparisons.c.
Complete the code for procedure recMerge2Arrays
.
For this work, the traditional algorithm can serve as a guide.
However, rather than declaring local variables larr
and rarr
in the merge procedure,
Run the program (with Approach 2 properly completed), and compare the run times for Approaches 1 and 2. Does using just two arrays rather than local arrays with each recursive call have any impact on program algorithm completion or on run times? Explain.
Complete the code for procedure iter2ArrMergeSort
.
For this work, the code for Approach 3 may provide some
guidance. In this code, for each outer loop, each array segment
will be merged with a second to give a larger array segment in
the second array. Thus, merging will go from the original array
to the second, the merging of segments in the second array will
move data to the first, etc. At the end, the completely sorted
data may be in either the original array or the auxiliary, so it
will be necessary to keep track which array is sorted, and copy
back to the original array at the end, if needed.
Run the entire program (with all approaches properly implement(, and compare the run times for Approach 4 with all other approaches. To what extent is Approach 4 better, the same, or worse than the others? Explain both what relative timings are observed and why those results are obtained.
Notes on Steps 3 – 8 Work submitted for Steps 3–8 of this assignment should combined as follows:
created December 1, 2018 revised December 2, 2018 revised December 27-30, 2021 revised February 4, 2022 reformatted and heap material added July 28, 2022 reorganized with brute force/merge sort added October 3-6, 2022 revised December 30, 2022 reorganized with moderate editing Summer, 2023 |
|
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu. |
Copyright © 2011-2022
by Henry M. Walker.
|