Description
Problem 1: (2 points) Give the asymptotic bounds for T(n) in the following recurrence. Make your
bounds as tight as possible and justify your answers.
?(?) = 3?(? −1) +1
Problem 2: (7 points) The ternary search algorithm is a modification of the binary search algorithm that
splits the input not into two sets of almost-equal sizes, but into three sets of sizes approximately onethird.
a) Verbally describe and write pseudo-code for the ternary search algorithm.
b) Give the recurrence for the ternary search algorithm
c) Solve the recurrence to determine the asymptotic running time of the algorithm. How does the
running time of the ternary search algorithm compare to that of the binary search algorithm.
Problem 3: (7 points) Design and analyze a divide and conquer algorithm that determines the minimum
and maximum value in an unsorted list (array).
a) Verbally describe and write pseudo-code for the min_and_max algorithm.
b) Give the recurrence.
c) Solve the recurrence to determine the asymptotic running time of the algorithm. How does
the theoretical running time of the recursive min_and_max algorithm compare to that of an
iterative algorithm for finding the minimum and maximum values of an array.
Problem 4: (4 points) 4-way Merge Sort
Consider a variation of Merge sort called 4-way Merge sort. Instead of splitting the array into two parts like
Merge sort, 4-way Merge sort splits the array into four parts. 4-way Merge divides the input array into fourths,
calls itself for each fourth and then merges the four sorted fourths.
a) Give pseudocode for 4-way Merge sort.
b) State a recurrence for the number of comparisons executed by 4-way Merge sort and solve to
determine the asymptotic running time.
Problem 5: (10 points)
a) Implement 4-way Merge sort from Problem 4 to sort an array/vector of integers and name it
merge4. Implement the algorithm in the same language you used for the sorting algorithms in
HW 1. Your program should be able to read inputs from a file called “data.txt” where the first
value of each line is the number of integers that need to be sorted, followed by the integers (like
in HW 1). The output will be written to a file called “merge4.txt”.
b) Modify code- Now that you have verified that your code runs correctly using the data.txt input
file, you can modify the code to collect running time data. Instead of reading arrays from the
file data.txt and sorting, you will now generate arrays of size n containing random integer values
from 0 to 10,000 to sort. Use the system clock to record the running times of each algorithm for
ten different values of n for example: n = 5000, 10000, 15000, 20,000, …, 50,000. You may need
CS 325 – Homework Assignment 2
to modify the values of n if an algorithm runs too fast or too slow to collect the running time
data (do not collect times over a minute). Output the array size n and time to the terminal.
Name the new program merge4Time.
c) Collect running times – Collect your timing data on the engineering server. You will need at least
eight values of t (time) greater than 0. If there is variability in the times between runs of the
same algorithm you may want to take the average time of several runs for each value of n.
Create a table of running times.
d) Plot data and fit a curve – Plot the data you collected on an individual graph with n on the x-axis
and time on the y-axis. You may use Excel, Matlab, R or any other software. What type of curve
best fits each data set? Give the equation of the curves that best “fits” the data and draw that
curves on the graphs.
e) Combine – Also plot the data from 4-way Merge sort together on a combined graph with your
results for merge and insertion sort from HW1.
Submit a copy of all your code files and a README file that explains how to compile and run your code
in a ZIP file to TEACH. We will only test execution with an input file named data.txt.