Finding number of Iterations [duplicate] - java

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.

Related

Time complexity for tree [duplicate]

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.

Pros and Cons of usage forEach and Stream [duplicate]

This question already has answers here:
What is difference between Collection.stream().forEach() and Collection.forEach()?
(5 answers)
Closed 5 years ago.
Starts from java 8 to iterate throug list I can use both:
List list = new ArrayList();
1. list.forEach(...)
2. list.stream().forEach(...)
Is it any advantages of using second case? To convert list to stream?
There are no advantages of using the second case, unless you have a parallel stream. There is a disadvantage, namely that Stream.forEach() doesn't guarantee to respect encounter order. A more accurate (but still unnecessary) equivalent would be Stream.forEachOrdered().
No, in theory, the second option is worse than the first one - you pay the cost of instantiation/garbage-collection and calling a Stream instance and don't really get any benefit in return.
Additionally, in theory the iteration order of Stream.forEach() isn't deterministic.

how to split a list into a given number of sub-lists? [duplicate]

This question already has answers here:
Is there a common Java utility to break a list into batches?
(21 answers)
Closed 6 years ago.
I have a list that may be over 1000 strings, however I do not know how many exactly.
What is the best way to split this list into smaller lists without loosing any members of the list?
For example If I have a list of 1323 members, how can I best split it into 3 almost evenly sized lists?
I have seen the Guava and Commons way of splitting lists by the partition function, but that function will split the list into given size of chunks and not given number of groups (sub-lists).
Guava has a function Lists.partition which will do this for you
Usage:
Lists.partition(mylist, mylist.size()/3);

How does contains in HashSet in Java run in O(1) time? [duplicate]

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

Generate all permutations in Java [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Generating all permutations of a given string
I have an array of arbitrary length in Java, and I would like to generate all possible permutations of them. The easy way to do this for a fixed length would be a series of nested for loops, but because the array is of unknown length, that is not an option here. Is there a straightforward way to accomplish this in Java?
Use a recursive function, instead of loops. Each time you call the method should be on a smaller portion of the array and stop when length = 0. This link should help you design your function.
It may or may not be optimal as far as performance goes, but if you're looking for a way to do it with writing relatively little code and having it be clear and maintainable, you want a recursive method rather than nested loops.

Categories

Resources