Part II - Determining Complexity of Algorithms
There are several types of complexities, which can be represented using the big-O notation, namely constant, linear, logarithmic, quadratic and exponential time.
Constant Time
Linear Time
Logarithmic Time
Quadratic Time
Other Time Complexities
There are other types of complexities such as factorial and exponential time. These algorithms are usually used for very small input sizes because it takes a very long time to execute. An example of an exponential time algorithm would be guessing a password.
Complexity Analysis and Data Structures
We can demonstrate how time complexity analysis can be applied to data structures by analyzing the run time of various methods of the data structures below.
Arrays
- Ex. Updating, indexOf(), Append(), .length()
- The time to perform these functions is independent of the array (input) size
Linear time or O(n) methods:
- Ex. find(), insert(), remove()
- The time it takes to perform a function is proportional to the size of the array because each of the above functions requires iterating over the entire array. This may not be obvious at first, but in the case of insert or remove, we need to iterate over the remainder of the array in order to move the elements one index over to the right or to the left, respectively
Linked List
- ex. Appending to the Linked List, Inserting, Removing*
- Appending, Inserting and Removing* can be done in one step
*Removing a node can be done in constant time in a Linked List if a bidirectional pointer is added to link back to the previous node. This would improve time complexity, however have negative implication on space complexity since it would require to store a pointer back to the node that pointed to it in the first place
Linear time or O(n) methods:
- Ex. Find, Length
- Finding the node or getting the length of the Linked List requires to iterate over every node from the beginning of the list until the end
For those of you learning how to code, I put together a curated list of resources that I used to learn how to code which can be found at breakingintostartups.com
Sign up for the Breaking Into Startups newsletter here for further guidance.
If you enjoyed this post, please share it with your friends! ❤ Also, I would love to hear about your stories in the comments below.