The science of runtime analysis is also present in data stru
Solution
Action
Complexity
Explanation
Adding an element in ArrayList
Amortized constant time
Its technically just an array
Adding element in HashSet
O(1)
Depends on initial capacity
Adding element in TreeSet
O(logn)
Iterative performance
Checking if LinkedList is empty
O(1)
As to check whether head node is present or not
Checking all nodes from LinkedList
O(n)
Depends upon number of nodes present
Checking if an ArrayList contains a given element
O(n) – Worst case
O(logn) – avg case
If we use Linear Search its worst case and avg case if we use Binary Search
Checking if an HashSet contains a given element
O(1)
As hash function takes its time complexity
Checking if an TreeSet contains a given element
O(logn)
Maximum height of a tree
Accessing the element of an ArrayList
O(n) – Worst case
O(logn) – avg case
Same as checking an item
Accessing the element of an LinkedList
O(n)
As we have to check all the elements
Adding the element at the beginning of an ArrayList
O(1)
As it takes small amount of time
Adding the element at the beginning of an LinkedList
O(1)
Just adding node to the head and making new node as head
| Action | Complexity | Explanation |
| Adding an element in ArrayList | Amortized constant time | Its technically just an array |
| Adding element in HashSet | O(1) | Depends on initial capacity |
| Adding element in TreeSet | O(logn) | Iterative performance |
| Checking if LinkedList is empty | O(1) | As to check whether head node is present or not |
| Checking all nodes from LinkedList | O(n) | Depends upon number of nodes present |
| Checking if an ArrayList contains a given element | O(n) – Worst case O(logn) – avg case | If we use Linear Search its worst case and avg case if we use Binary Search |
| Checking if an HashSet contains a given element | O(1) | As hash function takes its time complexity |
| Checking if an TreeSet contains a given element | O(logn) | Maximum height of a tree |
| Accessing the element of an ArrayList | O(n) – Worst case O(logn) – avg case | Same as checking an item |
| Accessing the element of an LinkedList | O(n) | As we have to check all the elements |
| Adding the element at the beginning of an ArrayList | O(1) | As it takes small amount of time |
| Adding the element at the beginning of an LinkedList | O(1) | Just adding node to the head and making new node as head |

