Order of growth according to Big-O Notation [duplicate] - java

This question already has answers here:
Big O, how do you calculate/approximate it?
(24 answers)
How can I find the time complexity of an algorithm?
(10 answers)
Closed 4 years ago.
Hello Stack Users, I am having trouble finishing this growth according to size algorithm problem. I was able to figure out the first two of the problem which are not listed in the picture. That would be 1. O(1) and 3. O(N) I was able to place these into their correct slots. I still cannot figure out how to determine the growth rate for 2,4,5,6 into the slots provided. Any suggestions on how to determine this?

O(N)
The first for loop takes N and the second also takes N so
O(N) = N + N = 2N = N
O(N^2)
The first for loop takes N and the second also N, but in this case it is nested. inner loop takes N for every other loop of outer loop
O(N) = N * N = O(N^2)
O(N)
The first for loop it takes N and the second also 5, but it is nested so
O(N) = 5 * N = 5N = O(N)
O(log(N))
to divide a number N by 2 continuously until it reaches 1, it takes
log(N)

Related

How to analyze the following algorithm complexity [duplicate]

This question already has answers here:
What is time complexity and how to find it? [duplicate]
(2 answers)
What is complexity of this code? (Big O) Is that linear?
(1 answer)
Closed 2 years ago.
I am new to algorithm analysis, so I appreciate if anyone can help me. I have the following algorithm for sorting an array:
for(int i = 0 ; i < list ; i++){
if(list[i] > list[i+1]){
swap list[i] with list[i+1]
i = -1;
}
}
I claim that this algorithm is a linear algorithm (i.e, O(n)) but I did not know how to prove this.
I appreciate any help.
Thanks in advance.
This algorithm is actually cubic (O(n^3) where n = length of list) in the worst case scenario. Imagine the following input: list = [5,4,3,2,1].
First iteration: list[0] > list[1]. The swap is made such that list = [4,5,3,2,1], and i is reduced to -1, so the loop starts over.
Second iteration: list[0] < list[1].
Third iteration: list[1] > list[2]. The swap is made such that list = [4,3,5,2,1], and i is reduced to -1, so the loop starts over.
Fourth iteration: list[0] > list[1]. The swap is made such that list = [3,4,5,2,1], and i is reduced to -1, so the loop starts over.
The same pattern continues: we will need 6 more iterations to bring 2 to the start of the list, 10 iterations for 1, and 5 to skim over the list once it's all sorted. Overall 4+6+10+5=25 which is 5^2. So why n^3 and not n^2?
Intuition:
For a list that sorted in reverse like the one in the above example, we would need to bring each element to the head of the list from greatest to smallest. The j'th element in the initial list is the j'th greatest overall and will need (1+2+...+j)=O(j^2) steps to bring to the head of the list.
Therefore, in order to reverse the list of length n, we need approximately (1^2 + 2^2 + ... + n^2) steps. That is the sum of squares from 1 to n which is O(n^3) - if you don't know why, it's a very well-known formula in arithmetics: sum of squares.
Disclaimer: Of course, it wouldn't be exactly n^3 steps, but it will be approximately so (which is after all the definition of Big-O notation. It will be closer to n^3 the bigger n gets).

Time complexity of the following program [duplicate]

This question already has answers here:
How can I find the time complexity of an algorithm?
(10 answers)
Closed 4 years ago.
So, basically I wanted to find all the elements in the second array which are less than or equal to the element of 1st array. Both the arrays are sorted. I know the solution. I don't want that.
I just want to know the time complexity of this program and how will we calculate it. Thank you in advance.
int count=0;
for(int i=0;i<n;i++)
{
for(int j=count;j<m;j++)
{
if(arr[i]>=arr[j])
//some O(1) code
else
{
count=j;
break;
}
}
}
The complexity will be O(n*m) simply because the outer loop for each value of n will run m times.
Well there is only one array in your code. In contrary to your explaination that says there are two arrays.
Assuming there is a typo and there should be a second array:
Worst: You can establish an upper bound at O(n * m). Happens if all elements in the second are smaler than in the first.
Best: You can establish a lower bound at O(n) .
Happens if all elements of the second are bigger than in the first(first element breaks the loop).
Average: If you asume an even distribution you get an average at O(n * m / 2).
Conclusion Its a O(n²) league algorithm.
Only one array:
However if I take the code "as is" - only one array and also take into account that it is sorted:
If arr1[i] < arr2[j] for i < j holds:
It will skip the inner loop for j>i. -> the inner loop will stop at j==i; -> upper bound at O(n * m / 2). Still an O(n²) league.
Reverse Order
So arr[i] < arr[j] for i>j holds:
It will skip the inner loop for j < i so the inner loop will be executed at most one time: O(n+m) rsp. O(n).
But I guess it is a typo and you ment two arrays so I skip the case sorted with duplicates(it is again O(n*m) eg. if all elements are the same).
O(n*m)- since you are going through 'n' outer elements
and for each outer element you have an inner loop with m elements.
For loops time complexity - O(n). Basically how many times the for loop will run.
Complexity : O(m*n)
As two for loops involved in this, it may be vary in different cases but it has to be O(m*n) if both gets executed.

How to calculate Time Complexity in nested for loop? [duplicate]

This question already has answers here:
Time complexity of nested for-loop
(10 answers)
Closed 5 years ago.
What is the time complexity of the following snippet? and could you explain it?
for(int i=0;i<n;i++){
for(int j=0;j<=i;j++){
//print something
}
}
The outer loop has n iterations.
The inner loop has i+1 iterations for each iteration of the outer loop.
Therefore the total number of iteration of the inner loop is:
1 + 2 + 3 + ... + n
which is equal to
n*(n+1)
-------
2
This is O(n^2)
Whenever you face a question of calculating time complexity, just look for how many times you are going to do the work.
Here, in your question, whatever the work you are doing, lets say printing something, you are going to do it for the the number of times of outer loop, which itself runs for n length.
hence, you will do the work for 1+2+3+....n times
which becomes
n*(n+1)/2
times.
Hence, it will simply be O(n^2)
For i=0, inner loop runs 1 time
For i=1, inner loop runs 2 times
For i=2, inner loop runs 3 times
...
n-1. For i=n-1, inner loop runs n times
So, total time = 1 + 2 + 3 +....+ n
n*(n+1)/2
and represented as O(n^2)
Time complexity is Big O of n square i.e. O(n^2)
For outer looper, it is n
Cost of inner loop is 1 + 2 + 3,...n-2, n-1, n
So total cost is O(n^2)
Quadratic Function (Quadratic Run Time)
An algorithm is said to be quadratic if
the main reason why the quadratic function appears is the nested loops, where the inner loop is performing a linear number of operations n and the outer loop is also performing a linear number of operations n, thus, in this case, the whole algorithm perform operations.
n is the number of elements you would have in any given array.
if the number of elements in your array is 10, this means it would take 10 * 10 = 100 operations to finish the execution of your logic.
A good example is to check how many a single item occurred in a list, this required comparing each single element in the list with all the element in the list including itself.
quadratic function graph:
I have an old BigO notation notes that covers the 7 most famous run time complexities see if this will help BigO

Longest subarray with sum less than equal to k [duplicate]

This question already has answers here:
Longest positive subarray
(2 answers)
Closed 6 years ago.
Given an array of positive integers a, and an integer k, I'm trying to figure out an algorithm which will give me the length of the longest subarray, the sum of which is less than or equal to k. I have figured out how to solve it in O(n^2) time, but am trying to solve in in as close to O(n) as I can.
For a O(n) solution, I'm trying to create a start index and an end index, which will give me a window. I want to check if the sum within this window is <= k AND if the length of this window is greater than the last recorded length. However, when typing it out, my logic breaks down.
I think you mean
maxLen = currLen;
I don't think its part of your problem but you don't use end and I don't think it's updated correctly. Just remove it.

How to estimate the time of Fibonacci recursive algorithm for nth element? [duplicate]

This question already has answers here:
Computational complexity of Fibonacci Sequence
(12 answers)
Closed 9 years ago.
How to estimate the time of completion of following algorithm for Nth Fibonacci element?
private static double fib(double nth){
if (nth <= 2) return 1;
else return fib(nth - 1) + fib(nth - 2);
}
The exact time complexity of this algorithm is... O(F(n)) where F(N) is nth Fibonacci number. Why? See the explanation below.
Let's prove it by induction. Clearly it holds for the base case (everything is a constant). Why does it hold for F(N)? Let's denote algorithm complexity function as T(N). Then T(N) = T(N-2) + T(N-1), because you make 2 recursive calls - one with the argument decreased by 1, one decreased by 2. And this time complexity is exactly Fibonacci sequence.
So F(N) is the best estimation you can make but you can also say this is O(2^n) or more precisely O(phi^n) where phi = (1 + sqrt(5)) / 2 ~= 1.61. Why? Because nth Fibonacci number is almost equivalent to phi ^ n.
This bound makes your algorithm non-polynomial and very slow for numbers bigger than something around 30. You should consider other good algorithms - there are many logarithmic algorithms known for this problem.

Categories

Resources