Time complexity of linear search in a loop? - loops

For I=1 to n
For J=1 to n
k = b[I]
F = Linear_search(a,k)
Print F
J=J*2
What will be the time complexity of above algorithm? I thought it would be O(nlogn) but there is a linear search too in algorithm which has complexity O(n).So what will be the complexity O(nlogn) or O(n) or will it be O(n^2 logn)?

There are :
n iterations for the first loop
log(n) iterations for the second
The program will call Linear_search nlog(n) times.
The complexity of the linear search being O(n), the complexity of the program will then be O(n^2log(n))

Related

What is the time complexity of this algorithm which retrieves values from a 2D array?

This is the algorithm to retrieve values from a 2D array, what will be the time complexity of this algorithm?
for(i=0;i<n;i++){
for(j=0;j<2;j++){
x = arr[i][j]
}
}
So the time complexity of this algorithm would be O(2n) or O(n^2)? What is the reason?
Outer loop is iterating n times, but inner loop is iterating only two times, So time complexity will be O(2n) and not O(n^2). And
O(2n) = O(n)

Time and Space Complexity of top k frequent elements in an array

There is a small confusion regarding the time and space complexity for the given problem:
Given an array of size N , return a list of top K frequent elements.
Based on the most popular solution:
Use a HashMap of size K with the count of each entry as value.
Build a MaxHeap of size K by traversing the HashMap generated above.
Pop the elements in the MaxHeap into a list and return the list.
K being the number of unique elements in the input.
The space and time complexity is: O(K) and O(K*log(K)
Now the confusion starts here. We know we are dealing with worst case complexity in the above analysis. So the worst value K can take is N, when all the elements in array are unique.
Hence K <= N. Thereby O(K) be represented as O(N) ??
Thereby, shouldn't the space and time complexity be O(N) and O(N*log(N)) for the above problem?
I know this is a technicality, but its been bothering me for a while. Please advise.
Yes, you are right since K<N, the time complexity for the hashmap part should be O(N).
But heap only have K elements in it and has the time complexity of O(Klog(K)) which if considered asymptotically is far larger than linear complexity of O(N) and hence results in final time complexity of O(Klog(K)).

Time Complexity of the C program

What will be the time complexity of the following function?
is it O(n^3) or O(n^4)?
i am getting O(n^3)
in the first for loop, it will undergo n times.
in the second forloop, for every nth element it will go n^2 times, therefore the total complexity till here is O(n^3)
now, the if statement will only hold true value only for n out of n^2 values, and for every n values the k- for loop will go till n^2 elements and hence the complexity is O(n^3).
I have taken few values of n:
for n=3 ,c=25
for n=10,c=1705
for n=50,c=834275
for(i=1;i<=n;++i)
for(j=1;j<=(i*i);++j)
if((j%i)==0)
for(k=1;k<=j;++k)
c=c+1;
Time complexity of such program is O(n^3) magnitude.

time complexities on while loop with condition of n*n

i=1;
while(i<n*n)
i=i+n;
from my lecturer provided answer:
Big-O notation was O(n) instead O(n^2) why?
Because after each loop run n is added to i. So it has to run maximal n times to reach n², thus ending the loop.
O(n^2) would be:
i=1;
while(i<n*n)
i=i+1;

Improving quicksort to sort in linear time?

Can we sort unsorted array of real numbers in linear time?
This is what I am thinking :
Given an unsorted array of size n :
Find out the sqrt(n)^th element using selection algorithm (call it x): O(n)
Find out the smaller elements than x and larger elements than x and form 2 arrays : O(n)
Sort the two arrays separately: O(sqrt(n)log(sqrt(n))) = O(n) as log(n) < sqrt(n)
So the whole algorithm is O(n)
I know that lower bound is nlog(n). What am I doing wrong?

Resources