What is the time complexity of the algorithm

Assignment Help Data Structure & Algorithms
Reference no: EM13188929

Question 1a: For n ? 0 , what is the time complexity of the method q(1, n). Show the details of your calculation of O(q(1, n) ˜ O(?).

public int q (int i, int n)
{
  return i+(i >= n ? 0 : q(i+i, n));
}

Answer: Linear time. O(n)

Question 1b: For n ? 0 , what is the time complexity of the r(n) method. Show the details of you calculation of O(r(n)) ˜ O(?).

public int r(int n)
{
    int sum = 0;
    for (int i=1; i <= n+n; i++)
    sum+=i + q(1,n);
    return sum;
}

Answer: Linear time. O(n)


Question 1c: For n ? 0 , what is the time complexity of the algorithm_1(int n) method. Show the details of you calculation of O(algorithm_1(n)) ˜ O(?).

public void algorithm_1(int n)
{
    if (n < 1) return; System.out.println(q(1, n)*n);
    System.out.println(r(n));
    System.out.println(q(1, n+n) + r(n+n));
}

Answer: Linear time. O(n)

Bonus:-

public int t(int n)
{
    for (int i=1; i <= n+n; i++)
{
    for (int j=1; j < i; j++)
    sum+=i + q(1,n);
}

Answer: Quadratic time. O(n^2)


Question 2:
n ? 0 , what is the time complexity of the binarySearch(array[n], key) algorithm. Show the details of you calculation of O(binarySearch(array[n], key)) ˜ O(?).

time = O(log n)

2^t -1 = n
2^t = n + 1
log2(2^t) = log2(n+1)
t = log2 n + 1

The time is logarithmic in the number of elements. Or proportional to the logarithm of # of elements.

Reference no: EM13188929

Questions Cloud

How much money will be join the savings account : A person deposited 500dollars in a savings account that pays 5% annual interest that is compound yearly. at the end of 10 years, how much money will be join the savings account.
What was the cost of the meal before the tip was added : Selena left an 18% tip for a meal. The total cost of the meal, including the tip, was $40.71. What was the cost of the meal before the tip was added?
Write the rate as a fraction in simplest form : Write the rate as a fraction in simplest form. 160 calories in an 8-fluid ounce serving of cream of tomato soup.
Choose a topic that focuses on one employees bad attitude : Write a letter that is no longer than 2 pages that responds to a customer complaint. You can either imagine that your company has received a complaint from a customer or client about a product, policy, or service, or you can work from a real life ..
What is the time complexity of the algorithm : what is the time complexity of the method and what is the time complexity of the algorithm - what is the time complexity of the binarySearch
Find the exact area of the surface obtained by rotating : Find the exact area of the surface obtained by rotating the curve about the x axis y=sinpiex/6,0
Find the mass and center of mass of the wire : A thin wire has the shape of the first-quadrant part of the circle with center the origin and radius c. If the density function is ρ(x, y) = kxy, find the mass and center of mass of the wire.
Are workplace appearance and grooming policies necessary : Are workplace appearance and grooming policies necessary? If so, what policy would you recommend with regard to office workers who have some contact with clients/customers? To what extent should the policy differentiate between male and female employ..
Determine the profit-maximizing output level : The following table gives the information regarding the units produced by one factory located in qatar. Complete the table and answer the questions below.Marginal Cost,Marginal Profit,Marginal Revenue,Total Profit for each Unit of output Total Rev..

Reviews

Write a Review

Data Structure & Algorithms Questions & Answers

  Calculate shortest path-djkstra-s shortest path algorithm

With indicated link costs, use Djkstra's shortest path algorithm to calculate shortest path from E to all network nodes. Illustrate how algorithm works by computing table.

  What is the worst case of avl tree?

the binary tree can look like a linked list in the worst case. What is the worst case of AVL tree? To get an idea, do the following: What is the minimum # of nodes in each of the AVL trees with heights 2, 3, 4, and 5?Explain please.

  Explain the sorting techniques selection sort

Explain the following sorting techniques using appropriate algorithms- (i) selection sort (ii) bubble sort

  Why there are no forward nontree edges

Explain why there are no forward nontree edges with respect to a BFS (breadth-first search) tree constructed for a direct graph.

  Auditing focuses on failures

Under normal situations, auditing focuses on failures to access rather than successful accesses. Explain why it might be a good concept to audit successful access to documents in a directory that contains highly confidential documents.

  Describe algorithm that finds maximum feasible flow in graph

Describe an algorithm that finds a maximum feasible flow in G. Denote by MF(|V|, |E|) the worst-case running time of an ordinary maximum flow algorithm.

  Professional codes of ethics

Select one of the Professional Codes of Ethics associated with IT. If you were to complete a assignment related to securing the connectivity in your firm and its business partners.

  Question about damaged database

Suppose if you were one of the users of a damaged database, discuss how would you be affected by such a failure and what measures could you take to prevent it?

  Preparing a java program

Prepare a program that asks the user how many automobiles are to be explained, and for each automobile it inputs the user's selection of make and color.

  Creating flowchart to compute and print the total sale

A coorporation's salesman are selling toothpaste and tooth powder. The corporation having fifty salesman gives 10% commission on the sale of toothpaste and 20 percent commission on tooth powder.

  Question about pointerlists

Whenever the pointer of a list or a tree is manipulated, procedure that performs this operation must be considered to be in a critical section.

  Normalized relations for a database

Suppose that a information communications network links a computer at corporate headquarters with a computer in each retail outlet. The chain includes fifty stores with an average of 75 workers per store.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd