Question

In: Computer Science

Discrete Math Problems: From the below algorithm (linear search) construct the function f(n) which computes the...

Discrete Math Problems:

  1. From the below algorithm (linear search) construct the function f(n) which computes the number of steps the algorithm executes for a list of n integers and compute O(f) using the definition

                    ALGORITHM 2 The Linear Search Algorithm.

                procedure linear search(x: integer, a1, a2,, an: distinct integers)

                i := 1

                while (in and xai)

               i := i + 1

               if in then location := i

               else location := 0

return location{location is the subscript of the term that equals x, or is 0 if x is not found}

  1. For the algorithm below, can you construct f(n) which gives the number of steps performed with input n if so, construct f(n); otherwise state why not.

// input to algorithm is n, an integer

j := n;

while( j>= 1 )

{

   for i := 1 to j

      {

          x := x + 1;

       }

    j := floor ( j/2 );

}

Solutions

Expert Solution

Given:

ALGORITHM 2 The Linear Search Algorithm.

                procedure linear search(x: integer, a1, a2,, an: distinct integers)

                i := 1

                while (in and xai)

               i := i + 1

               if in then location := i

               else location := 0

return location{location is the subscript of the term that equals x, or is 0 if x is not found}

Linear Search number of steps:

If n = 1, number of steps= 1

If n = 2, number of steps= 2

If n=n, number of steps= n

So, f(n)=n.

Again,

// input to algorithm is n, an integer

j := n;

while( j>= 1 )

{

   for i := 1 to j

      {

          x := x + 1;

       }

    j := floor ( j/2 );

}

So, for n=1 number of steps=1

...

for n=8,

f(n)= 4 (while loop) + (8 + 4 + 2 + 1)(for loop) = 19

for n=n,


Related Solutions

Construct a TM that computes the function: f(x) = 2x. (first give a brief outline, then...
Construct a TM that computes the function: f(x) = 2x. (first give a brief outline, then show how it works for x=11 (in unary system, not eleven) using instantaneous description.
T/F 1) The function f(x) = x1 − x2 + ... + (−1)n+1xn is a linear...
T/F 1) The function f(x) = x1 − x2 + ... + (−1)n+1xn is a linear function, where x = (x1,...,xn). 2) The function f(x1,x2,x3,x4) = (x2,x1,x4,x3) is linear. 3) For a given matrix A and vector b, equation Ax = b always has a solution if A is wide
Design a Turing Machine to construct the function f(n) = 4 [1/4 n] + 2, (that...
Design a Turing Machine to construct the function f(n) = 4 [1/4 n] + 2, (that is, 2 more than 4 times the integer part of 1/4 n) for n Element N. Do not just produce a TM, but also describe briefly how it works. There is a TM in the Cooper notes that does something similar. You may modify it to produce the required TM, or produce a machine totally independently.
1. Which of the following is the linear approximation of the function f ( x )...
1. Which of the following is the linear approximation of the function f ( x ) = 2e^sin (7x) at x = 0? Group of answer choices y=cos⁡(7)x+2 y=7x+2 y=14x+2 y=2x+7 y=e^7x+14 2. Recall that Rolle's Theorem begins, ``If f ( x ) is continuous on an interval [ a , b ] and differentiable on (a , b) and ___________, then there exists a number c …'' Find all values x = c that satisfy the conclusion of Rolle's...
Recall the truncated distribution function F* and the algorithm for generating from it, as given in...
Recall the truncated distribution function F* and the algorithm for generating from it, as given in Sec. 8.2.1. (a) Show that the algorithm stated in Sec. 8.2.1 is valid when F is continuous and strictly increasing. (b) Show that the following algorithm is also valid for generating X with distribution function F* (assume again that F is continuous and strictly increasing): 1. Generate U , U(0, 1). 2. If F(a) # U # F(b), return X 5 F21(U). Otherwise, go...
1. Which of the following is(are) required condition(s) for a discrete probability function? ∑f(x) = 0...
1. Which of the following is(are) required condition(s) for a discrete probability function? ∑f(x) = 0 f(x) ≥ 1 for all values of x f(x) < 0 None of the answers is correct. 2.Let Z be the standard normal random variable. What is P(0<Z<2.50)? 0.4640 0.4938 0.3519 0.4028 None of the above 3. A researcher has collected the following sample data. 5 12 7 9 5 6 7 5 13 4 The 90th percentile from Excel Functional work is 12...
The table below contains data for a linear function: tt 6.2 6.4 6.6 6.8 f(t)f(t) 565.2...
The table below contains data for a linear function: tt 6.2 6.4 6.6 6.8 f(t)f(t) 565.2 577.4 589.6 601.8 Find a formula for the function. f(t)=
Consider a random sample of size n from a distribution with function F (X) = 1-...
Consider a random sample of size n from a distribution with function F (X) = 1- x-2 if x > 1 and zero elsewhere. Determine if each of the following sequences has distribution limit; if so, give the limit distribution. a)x1:n b)xn:n c)n-1/2 xn:n
1- Write a function f(n,a,b) that generates n random numbers # from Uniform(a,b) distribution and returns...
1- Write a function f(n,a,b) that generates n random numbers # from Uniform(a,b) distribution and returns their minimum. # Execute the function with n=100, a=1, b=9. 2- Replicate the function call f(100,1,9) 100 thousand times # and plot the empirical density of the minimum of 100 indep. Unif(1,9)'s 3-Use the sampling distribution from (b) to find 95% confidence # interval for the minimum of 100 independent Unif(1,9)'s. Please solve in R
A rock is thrown upward from a bridge into a river below. The function f(t)=−16t^2+36t+136 determines...
A rock is thrown upward from a bridge into a river below. The function f(t)=−16t^2+36t+136 determines the height of the rock above the surface of the water (in feet) in terms of the number of seconds t since the rock was thrown. What is the bridge's height above the water? How many seconds after being thrown does the rock hit the water? How many seconds after being thrown does the rock reach its maximum height above the water? What is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT