Solved-HW 1 -Solution

$30.00 $19.00

(1 pt) Suppose we are comparing implementations of insertion sort and merge sort on the same machine. For inputs of size n, insertion sort runs in 8n2 steps, while merge sort runs in 64nlgn steps. For what values of n does insertion sort run faster than merge sort? Note: lg n is log “base 2”…

You’ll get a: . zip file solution

 

 
Categorys:
Tags:

Description

5/5 – (2 votes)
  1. (1 pt) Suppose we are comparing implementations of insertion sort and merge sort on the same

machine. For inputs of size n, insertion sort runs in 8n2 steps, while merge sort runs in 64nlgn steps. For what values of n does insertion sort run faster than merge sort?

Note: lg n is log “base 2” of n or log2 . There is a review of logarithm definitions on page 56. For most calculators you would use the change of base theorem to numerically calculate lgn.

  1. (5 pts) For each of the following pairs of functions, either f(n) is O(g(n)), f(n) is Ω(g(n)), or f(n) =

Θ(g(n)). Determine which relationship is correct and explain.

a.

f(n) = n0.25;

g(n) = n0.5

b.

f(n) = n;

g(n) = log2 n

c.

f(n) = log n;

g(n) = ln n

d.

f(n) = 1000n2;

g(n) = 0.0002n2 – 1000n

e.

f(n) = nlog n;

g(n) =n

f.

f(n) = en;

g(n) = 3n

g.

f(n) = 2n;

g(n) = 2n+1

h.

n

g(n) =2

2

f(n) = 2 ;

i.

f(n) = 2n;

g(n) = n!

j.

f(n) = lgn;

g(n) =

  1. (4 pts) Let f1 and f2 be asymptotically positive non-decreasing functions. Prove or disprove each of the following conjectures. To disprove give a counter example.

    1. If f1(n) = (g(n)) and f2(n) = (g(n)) then f1(n)= (f2(n) ).

    1. If f1(n) = O(g1(n)) and f2(n) = O(g2(n)) then f1(n)+ f2(n)= (g1(n) + g2(n) )

  1. (10 pts) Merge Sort and Insertion Sort Programs

Implement merge sort and insertion sort to sort an array/vector of integers. You may

implement the algorithms in C, C++ or Python, name the programs “mergesort” and

insertsort”. Your programs 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.

Example values for data.txt:

4 19 2 5 11

8 1 2 3 4 5 6 1 2

The output will be written to files called “merge.out” and “insert.out”.

For the above example the output would be:

2 5 11 19

1 1 2 2 3 4 5 6

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 test execution with an input file named data.txt.

  1. (10 pts) Merge Sort vs Insertion Sort Running time analysis

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

Submit a copy of the timing programs to TEACH in the Zip file from problem 4, also include a “text” copy of the modified timing code in the written HW submitted in Canvas.

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

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

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

  1. 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. Submit your code to TEACH in a zip file with the code from Problem 4 & 5.