This question already has answers here:
How can I find the time complexity of an algorithm?
(10 answers)
Closed 3 months ago.
I'm having trouble finding the time complexity of a function.
This is the function:
int Mystery(Node root){
if(root==null)
return null;
if(root.leftchild==null)
return null;
return Mystery(root.leftchild)
}
what made me get confused is that there are 2 base conditions
this is what I wrote:
t(n)=t(n/2)+2 (2 stands for the 2 if conditions)
t(n)=t(n/4)+2+2
t(n)=t(n/8)+2+2+2
t(n)=t(n/2^k)+2*k
assume n/2^k=1
so k=log(n)
t(n)=t(1)+2log(n)
so O(log(n))
Is this correct or is there something missing?
There are just two base cases, but each of them imply that n has reached some small, bounded number -- assuming the tree is balanced.
So as long as the tree is balanced, your logic is correct.
Related
This question already has answers here:
How does Recursion actually work in Java?
(4 answers)
Closed 2 years ago.
I just started recursion and happened across a function similar to this, so I adapted it to the question on my homework:
public int getRSquare(int n){
if(n==0){
return 1;
}
return getRSquare(n-1)*n;
}
So when n=5, it returns 120, which is correct, this method outputs the factorial of n. But I don't understand WHY. Doesn't n eventually reach 0, so shouldn't it return 1, not 120? This is java btw, just in case that's needed
on the return value n is updated each time when the method is called. 5-1=4-1=3-1=2-1=2-1=0.
When n is 0 the method stop.
This question already has answers here:
How can I find the time complexity of an algorithm?
(10 answers)
Closed 4 years ago.
What will be the time complexity of the following code?
for(i=0;i<=n;i++)
for(j=0;j<=log i;j++)
print("hello world");
Summing up the number of times the inner loop is iterating, we get,
log1 + log2 + log3 + log4 + ... + logN
= log(N!)
And according to Stirling's approximation, log(N!) = O(N x log(N))
So the time complexity is O(NlogN)
This question already has answers here:
What is the time complexity of java.util.Collections.sort() method?
(4 answers)
Closed 4 years ago.
I am trying to find the number of iterations an algorithm does when sorting.
How we find the number of iterations built in sort:Collections.sort does when sorting?
I don't think you can count the number of iterations at run time. (You could count them using a debugger, but I don't think that's what you mean.)
You can count the number of comparisons done by Collections.sort() by using the two-argument version of the method and passing your own comparator that counts how many times it is called. That's probably a more meaningful measure of sorting work than counting iterations.
This question already has answers here:
How to print binary tree diagram in Java?
(22 answers)
Closed 7 years ago.
I've created an AVL Tree, with working Add and Remove methods. However, I need to print out the tree in a visual format. something like this:
Image
is there a relatively short way of doing so?
(You can assume that i have the height of every node).
I doubt there's any easy way to display the data in an AVL tree besides inorder, postorder, and preorder list representations.
However, I did find this solution on an old Stack Overflow question.
This question already has answers here:
Is a Java hashmap search really O(1)?
(15 answers)
Closed 8 years ago.
Is it only O(1) when there are no collision. Im talking about a hash table that has Linked Lists in each slot to hold the values.
The average number of collisions is O(1), and if your hash function is essentially random you can prove that it's extremely improbable that there are many collisions.
Yes it is O(1) if you have unique hash for keys and LinkedList or Binary Tree has only one item,
With Java 7 collision resolves to binary tree instead of LinkedList so it is not O(N) for collision