CS 415, Section 001 Sonoma State University Spring, 2023
 
Algorithm Analysis
Instructor: Henry M. Walker

Lecturer, Sonoma State University
Professor Emeritus of Computer Science and Mathematics, Grinnell College

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.

Assignment on Brute Force Algorithms and Merge Sort

This assignment is in two parts: Brute Force Algorithms and Merge Sort

Brute Force Algorithms

This section on contains two exercises related to brute force algorithms.

  1. 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.

  2. 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.
    Notes:

Merge Sort

This section compares four implementations of a Merge Sort.

Program mergesort-comparisons.c provides a framework for the development of a program to implement and test each of these implementations.

  1. Review program mergesort-comparisons.c.

    1. Explain the purpose of the sortProcs array. What information does it hold, how is information used, and why is it needed?
    2. What is the purpose of the variables maxDataSetSize, maxRecSize, and algTimeLimit, why are they needed, how are their values set, and how are they used?
    3. What is the purpose of the arrays algAscActive, algRanActive, and algDesActive, how are their values computed, and how are they used?
  2. Run program mergesort-comparisons.c.

    1. Describe the output produced.
      • What timings are listed?
      • Some timings are followed by "ok", some by "Er", and some by "NO"? Why does "ok" appear for Approaches 1 and 3 and for the other algorithms working on data in ascending order? Why does "Er" appear for Approaches 2 and 4 for random data, but "NO" for descending data?
    2. In the program, the main loop goes to 40960000, but a segmentation fault is generated after size 1600000. Why do you think this outcome arises?
    3. Edit the code, so that the main loop runs through all data sizes up to 40960000, except that Approach 1 is not called for array sizes above 1600000 (only dashes are printed to maintain the layout of the table). For this edit, explain why only one token in the code must be changed.
    4. With the code updated, examine the times involved for Approach 3.
      • How does the time for ascending or descending data compare to the time for random data? In analyzing the algorithm, why do you think this outcome might occur?
      • When the size of the array doubles, what can you say about how the time for sorting increases? To what extent is this consistent with the Big-Θ analysis?
  3. 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,

  4. 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.

  5. 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.

  6. 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
Valid HTML 4.01! Valid CSS!
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu.
ccbyncsa.png

Copyright © 2011-2022 by Henry M. Walker.
Selected materials copyright by Marge Coahran, Samuel A. Rebelsky, John David Stone, and Henry Walker and used by permission.
This page and other materials developed for this course are under development.
This and all laboratory exercises for this course are licensed under a Creative Commons Attribution-NonCommercial-Share Alike 4.0 International License.