I am confused with the time complexity of these two algorithms.
//time complexity O(nlog(n))
public void usingTreeMap(){
Map map = ne
In the case of HashMap, the backing store is an array. When you try to insert ten elements, you get the hash, compute the specific array index from that hash, and since it's an array in the back, you inject in O(1).
So, total time for insertion of n elements in a HashMap = n * O(1) = O(n)
In this case, the backing store is a Tree. For a tree with total k elements, on an average, the time to find the location is O(Log k).
Total time = Log 1 + Log 2 + Log 3 + ... + Log (n-1)
Now, Log 1 <= Log n, Log 2 <= Log n ... Log (n-1) <= Log n, leading us to n-1 values each of which is less than or equal to Log n.
This means that the timing for insertion in a treemap sum to a value <= (n-1) * Log (n), leading to the complexity of O(n Log (n)).
One of the properties of logs is Log a + Log b = Log (ab)
. Using that, the insertion time in case of TreeMap sums to a lesser-known running time value of O(Log(n!)). But, since, O(Log(n!)) is bound by O(n Log(n)), the time complexity of insertion of n elements in a TreeMap is loosely written O(n Log(N)).