Description
Problem 1. (4 points)
Order the following functions by growth rate: π, βπ, π
1.5
, π
2
, πππππ, ππππππππ,
ππππ2π, ππππ(π
2)
, π/2, 2
π, 2
π/2
, 37, π
2
log N, π
3
. Indicate which functions grow at the same
rate.
Problem 2. (3 points)
Which function grows faster: πππππ or π
1+Ι/βππππ, Ι>0? Show your work.
Problem 3. (3 points)
For each of the following six program fragments, give an analysis of the running time (Big-Oh
will do).
(1) sum = 0;
for( i = 0; i < n; ++i )
++sum;
(2) sum = 0;
for( i = 0; i < n; ++i )
for( j = 0; j < n; ++j )
++sum;
(3) sum = 0;
for( i = 0; i < n; ++i )
for( j = 0; j < n * n; ++j )
++sum;
(4) sum = 0;
for( i = 0; i < n; ++i )
for( j = 0; j < i; ++j )
++sum;
(5) sum = 0;
for( i = 0; i < n; ++i )
for( j = 0; j < i * i; ++j )
for( k = 0; k < j; ++k )
++sum;
(6) sum = 0;
for( i = 1; i < n; ++i )
for( j = 1; j < i * i; ++j )
if( j % i == 0 )
for( k = 0; k < j; ++k )
++sum;
Problem 4. (10 points)
Merge Sort and Insertion Sort Programs
Implement merge sort and insertion sort to sort an array/vector of integers. Name your programs
βmergesortβ and βinsertsortβ, respectively. Both programs should 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. The output will be written to files called βmerge.outβ and βinsert.outβ.
Example values for data.txt:
4 19 2 5 11
8 1 2 3 4 5 6 1 2
For the above example the output would be:
2 5 11 19
1 1 2 2 3 4 5 6
Problem 5. (10 points)
Merge Sort vs Insertion Sort Running time analysis
a) 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 n = 5000, 10000, 15000, 20,000β¦. You may need to modify the values of
n if an algorithm runs too fast or too slow to collect the running time data. Output the array
size n and time to the terminal. Name these new programs βinsertTimeβ and βmergeTimeβ.
b) Collect running times – Collect your timing data on the engineering server. You will need at
least seven 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 for each algorithm.
c) Plot data and fit a curve – For each algorithm plot the running time data you collected on an
individual graph with n on the x-axis and time on the y-axis. You may use Excel, Matlab, 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.
d) Combine – Plot the data from both algorithms together on a combined graph. If the scales are
different you may want to use a log-log plot.
e) Comparison – Compare your experimental running times to the theoretical running times of
the algorithms? Remember, the experimental running times were the βaverage caseβ since the
input arrays contained random integers.
EXTRA CREDIT: A Tale of Two Algorithms: It was the best of times, it was the worst of
times β¦
Generate best case and worst case inputs for the two algorithms and repeat the analysis in parts
b) to e) above. To receive credit you must discuss how you generated your inputs and your
results.
Programs can be written in C, C++ or Python but all code must run on the
OSU engr servers. 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.