Homework: #1 Solution

$35.00 $24.00

1) (3 pts) Describe a 0(n lgn) time algorithm that, given a set S of n integers and another integer x, determines whether or not there exist two elements in S whose sum is exactly x. Explain why the running time is e(n lgn).     (3 pts) For each of the following pairs of…

You’ll get a: . zip file solution

 

 
Categorys:

Description

5/5 – (2 votes)

1) (3 pts) Describe a 0(n lgn) time algorithm that, given a set S of n integers and another integer x, determines whether or not there exist two elements in S whose sum is exactly x. Explain why the running time is e(n lgn).

 

 

(3 pts) For each of the following pairs of functions, either f(n) is O(g(n)), f(n) is O(g(n)), or f(n) =

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

 

 

  1. f(n) = n°·2 g(n) = n°.s

 

 

  1. f(n) = log n2; g(n) = In n
  2. f(n) = nlog n; g(n) =min
  3. d. f(n) = 4n; g(n) = 3n
  4. f(n) = 2n; g(n) = 2n+l
  5. f(n) = 2n; g(n) = n!

 

 

 

 

(4 pts) Let f1and f2  be asymptotically positive non-decreasingfunctions.Prove or disprove each of the following conjectures. To disprove give a counter example.

 

  1. If jj(n ) = O(g(n)) and fi n) = O(g(n)) thenjj(n) +f2(n ) = O(g(n)).

 

  1. If j{n) = O(gJ(n)) and j{n ) = O(g2(n)) then gJ(n) = 0(g2(n) )

 

4) (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 the language of your choice, name one program “mergesort” and the other “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:

1.                  4192511

812345612

The output will be written to files called “merge.out” and “insert.out”. For the above example the output would be:

2.              2511 19

11223456

 

5) (10pts) Merge Sort vs Sort Running Time analysis

 

The goal of this problem is to compare the experimental running times of the two sorting algorithms

 

 

  1. Now that you have proven that your code runs correctly using the data.txt input file, you can modify the code to collect runningtime Instead of reading arrays from a file to sort,you will now generate arrays of size n containing random integer values from 0 to 10,000 and then time how long it takes to sort the arrays. We will not be executing the code that generates the runningtime data so it does not have to be submitted to TEACH or even execute on flip. Include a text” copy of the modified code in the written HW submitted in Canvas.

 

  1. c) For each algorithm plot the running time data you collected on an individua lgraph with n on the x-axis and time on the y-axis. You may use Excel,Matlab, R or any other software. Also plot the data from both algorithms together on a combined Which graphs represent the data

best?

 

 

  1. d) What type of curve best fits each data set? Again you can use Excel, Matlab,any software or a graphing calculator to calculate a regression Give the equation of the curve that best “fits” the data and draw that curve on the graphs of created in part c).

 

  1. e) How do your experimental running times compare to the theoretical running times of the

Algorithms?  Remember, the experimental running times were “average case” since the input arrays contained random integers.