Big O of Java Collections
All credit goes to Kunal saxena.
Resource Link: https://www.linkedin.com/pulse/big-o-java-collections-kunal-saxena/
| Time complexity | Name | Example |
| 1 | Constant | Adding an element to the front of a linked list |
| logn | Logarithmic | Finding an element in a sorted array |
| n | Linear | Finding an element in an unsorted array |
| nlogn | Linear Logarithmic | Sorting n items by „divide-and-conquer‟-Mergesort |
| n^2 | Quadratic | Shortest path between two nodes in a graph |
| n^3 | Cubic | Matrix Multiplication |
| 2^n | Exponential | The Towers of Hanoi problem |
"madam", this algorithm would make the following comparisons:m ↔ m a ↔ a d ↔ d a ↔ a m ↔ mAll that needs to be compared to prove it’s a palindrome are the first two characters against the last two since the middle one does not need to be checked:
m ↔ m a ↔ aThis leads us to our initial solution:
function isPalindrome (text)
if text is null
return false
left ← 0
right ← text.length - 1
while (left < right)
if text[left] is not text[right]
return false
left ← left + 1
right ← right - 1
return true
Resource Link:
http://www.growingwiththeweb.com/2014/02/determine-if-a-string-is-a-palindrome.html
2. http://www.growingwiththeweb.com/2012/11/big-o-notation.html
If there is a single iteration, and the iterative variable is incrementing linearly then it's O(n) e.g.
for(i=0;i<n;i++) //O(n)
for(i=0;i<n;i = i + 4) // still O(n)
If the iterative variable is incremented geometrically, then it's O(log n)
e.g
for(i=1;i<n;i = i * 2) //O(log n)
Note that, the implementations don't have to be using loops, they maybe implemented using recursion.
If there is nested loop, where one has a complexity of O(n) and the other O(logn), then overall complexity is O(nlogn);
e.g
for(i=0;i<n;i++) // O(n)
{
for(j=1;j<n;j=j*3) // O(log n)
}
//Overall O(nlogn)
This is only a finger cross guideline. In general, you have to have good concept to derive the complexity.