This question already has answers here:
How can I find the time complexity of an algorithm?
(10 answers)
Closed 5 years ago.
The steps of algorithm are:
Set J = N
Repeat while J >1
Module A.
J = J/2
Return.
I need to find the time complexity of following in Big O notation.
Assuming Module A is a O(k) operation you observe that J is decreasing exponentially most specifically you observe that the loop will always reapeat log n (base 2 because it is halfed at each iteration) times thus you have a time complexity of O(k*log n)
Related
This question already has answers here:
The running time of Insertion-Sort
(3 answers)
Closed 1 year ago.
Suppose that you run the code fragment below (generate and then insertion-sort an array of numbers) for n=10,000 and observe that it takes 5.3 seconds. Which of the following is a reasonable prediction of its running time (in seconds) for n=1,000,000?
insertionsort([i for i in range(n)])
We know (If you don't know yet - read algorithmic books immediately) that the worst and average time for insertion sort is quadratic, i.e. execution time depends on input length as O(n^2) (squared n).
For rather large n values we can write
T = k * n^2
and find constant k from given n (10000 in your case) and time T (5.3 seconds).
Then use the same equation with calculated k constant and another n value (1,000,000) to get estimated time.
Small remark: there is possibility that your code (unseen) does generate almost sorted array. In this case there are nuances....
This question already has answers here:
How can I find the time complexity of an algorithm?
(10 answers)
Big O, how do you calculate/approximate it?
(24 answers)
Closed 5 years ago.
Hello, I have 2 functions in the image. And the question is about getting their runtime.
The answer for EX4 function is O(n^2) and EX5 is O(n^4).
I don't get this.
Question for EX4:
we have inner loop that starts with j=0 to i. From my perspective, this is equivalent to "1+2+...+n", so is "n(n+1)/2", therefore, O(n^2) for inner loop only.
However, we also know that outer loop runs from i=0 to n, which is O(n). So the answer I thought for EX4 was actually "O(n) * O(n^2) = O(n^3)". But the real answer says O(n^2). Why is that?
Question for EX5:
Similarly, I thought it is "n*(n+1)/2 = O(n^2)" for inner loop and also "n*n=O(n^2)" for outer loop, so the whole runtime become O(n^2) * O(n^2) = O(n^4), which is same as real answer of this question. But if I justify this in this way, then my solution to EX4 doesn't make sense. Why is it O(n^4) specifically?
This question already has answers here:
What is a plain English explanation of "Big O" notation?
(43 answers)
Closed 5 years ago.
I have just started "Cracking the Coding Interview" by Gayle Macdowell. In this BigO topic, It says we should drop the non-dominant term.
O(n^2 + n) becomes O(n^2) and O(n + log n) becomes O(n).
Well, I understand that. If we suppose the value of n to be some large number then we can ignore the smaller result since, it will be comparatively much more smaller than the larger one.
But, in this case how can O(5*2^n + 1000n^100) become O(2^n)) ?
Isn't n^100 dominant than 2 ^n ?
n^100, or n raised to any constant, does not dominate 2^n.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Original close reason(s) were not resolved
Improve this question
What is the smallest value of n such that an algorithm whose running time is 100n^2 runs faster than an algorithm whose running time is 2^n on the same machine?
The Scope
Although I am interested in the answer, I am more interested in how to find the answer step by step (so that I can repeat the process to compare any two given algorithms if at all possible).
From the MIT Press Algorithms book
You want the values of n where 100 × n2 is less than 2 × n.
Which is the solution of 100 × n2 - 2 × n < 0, which happens to be 0 < n < 0.02.
One thousand words:
EDIT:
The original question talked about 2 × n, not 2n (see comments).
For 2n, head to https://math.stackexchange.com/questions/182156/multiplying-exponents-solving-for-n
Answer is 15
The first thing you have to know, is what running time means. If we're talking about algorithms theoretically, the running time of an algorithm is the number of steps (or the amount of time) it takes to finish depending on the size of the input (where the size of the input is for example the number of bits, but also other measures are sometimes considered). In this sense, the algorithm which requires the least number of steps is the fastest.
So in your two formulas, n is the size of the input, and 100 * n^2 and 2^n are the number of steps the two algorithms run if given an input of size n.
On first sight, the 2^n algorithm looks much faster than the 100 * n^2 algorithm. For example, for n = 4, 100*4^2 = 1600 and 2^4 = 16.
However, 2^n is an exponential function, whereas 100 * n^2 is a polynomial function. That means that when n is large enough, it will be the case that 2^n > 100 * n^2. So you will have to solve the unequality 100 * n^2 < 2^n. This will already be the case for a fairly small n, so you can just start evaluating the functions, starting at n=5, and you will reach the answer to the question in a few minutes.
This question already has answers here:
What does O(log n) mean exactly?
(32 answers)
Closed 8 years ago.
Usually the rule is that if there is a loop of 1 to n elements then the complexity is O(n), and further nested loops are n x O(n). However, when do we say if a subroutine has complexity O(log n)?
When in each iteration we reduce the problem size be a factor of X , we can say that the problem is O(log n)
E.g - Binary Search: in each iteration we reduce the problem size by factor of 2
You can take as first example binary search. A explanation of the complexity of this algorithm can be taken from a related question how to calculate binary search complexity. It shown that the calculation of this type of complexity can be obtain from the recurrence.