Analysis of Loops - Complexity
Analysis of Loops - Complexity
GeeksforGeeks
A computer science portal for geeks
GeeksQuiz
Login
Home
Algorithms
DS
GATE
Interview Corner
Q&A
C
C++
Java
Books
Contribute
Ask a Q
About
Array
Bit Magic
C/C++
Articles
GFacts
Linked List
MCQ
Misc
Output
String
Tree
Graph
1) O(1): Time complexity of a function (or set of statements) is considered as O(1) if it doesn’t contain
loop, recursion and call to any other non-constant time function.
// set of non-recursive and non-loop statements
1 of 6 08/03/2014 00:09
Analysis of Algorithms | Set 4 (Analysis of Loops) | GeeksforGeeks http://www.geeksforgeeks.org/analysis-of-algorithms-set-4-analysis-of-...
2) O(n): Time Complexity of a loop is considered as O(n) if the loop variables is incremented /
decremented by a constant amount. For example following functions have O(n) time complexity.
// Here c is a positive integer constant
for (int i = 1; i <= n; i += c) {
// some O(1) expressions
}
3) O(nc): Time complexity of nested loops is equal to the number of times the innermost statement is
executed. For example the following sample loops have O(n2) time complexity
For example Selection sort and Insertion Sort have O(n2) time complexity.
4) O(Logn) Time Complexity of a loop is considered as O(Logn) if the loop variables is divided /
multiplied by a constant amount.
for (int i = 1; i <=n; i *= c) {
// some O(1) expressions
}
for (int i = n; i > 0; i /= c) {
// some O(1) expressions
}
For example Binary Search(refer iterative implementation) has O(Logn) time complexity.
5) O(LogLogn) Time Complexity of a loop is considered as O(LogLogn) if the loop variables is reduced /
increased exponentially by a constant amount.
// Here c is a constant greater than 1
for (int i = 2; i <=n; i = pow(i, c)) {
// some O(1) expressions
}
//Here fun is sqrt or cuberoot or any other constant root
for (int i = n; i > 0; i = fun(i)) {
// some O(1) expressions
}
2 of 6 08/03/2014 00:09
Analysis of Algorithms | Set 4 (Analysis of Loops) | GeeksforGeeks http://www.geeksforgeeks.org/analysis-of-algorithms-set-4-analysis-of-...
loops.
for (int i = 1; i <=m; i += c) {
// some O(1) expressions
}
for (int i = 1; i <=n; i += c) {
// some O(1) expressions
}
Time complexity of above code is O(m) + O(n) which is O(m+n)
If m == n, the time complexity becomes O(2n) which is O(n).
How to calculate time complexity when there are many if, else statements inside loops?
As discussed here, worst case time complexity is the most useful among best, average and worst.
Therefore we need to consider worst case. We evaluate the situation when values in if-else conditions
cause maximum number of statements to be executed.
For example consider the linear search function where we consider the case when element is present at
the end or not present at all.
When the code is too complex to consider all if-else cases, we can get an upper bound by ignoring if else
and other complex control statements.
Please write comments if you find anything incorrect, or you want to share more information about the
topic discussed above.
Related Tpoics:
Analysis of Algorithms | Set 3 (Asymptotic Notations)
NP-Completeness | Set 1 (Introduction)
Static and Dynamic Libraries | Set 1
The Ubiquitous Binary Search | Set 1
Reservoir Sampling
Analysis of Algorithms | Set 2 (Worst, Average and Best Cases)
Analysis of Algorithms | Set 1 (Asymptotic Analysis)
3 of 6 08/03/2014 00:09
Analysis of Algorithms | Set 4 (Analysis of Loops) | GeeksforGeeks http://www.geeksforgeeks.org/analysis-of-algorithms-set-4-analysis-of-...
Scope rules in C
Like 12 Tweet 4 1
Writing code in comment? Please use ideone.com and share the link here.
GeeksforGeeks
Like
Popular Posts
All permutations of a given string
4 of 6 08/03/2014 00:09
Analysis of Algorithms | Set 4 (Analysis of Loops) | GeeksforGeeks http://www.geeksforgeeks.org/analysis-of-algorithms-set-4-analysis-of-...
657
Subscribe
Recent Comments
RajKumar Rampalli
Rearrange a string so that all same characters become at least d distance away · 48 minutes
ago
uuuouou
5 of 6 08/03/2014 00:09
Analysis of Algorithms | Set 4 (Analysis of Loops) | GeeksforGeeks http://www.geeksforgeeks.org/analysis-of-algorithms-set-4-analysis-of-...
Rearrange a string so that all same characters become at least d distance away · 1 hour ago
veer
Vinay
1,1,2,3,4,5,6,2,2,2,5,3,3,2,2,1,5,5,5,5,4,4,4,1...
neelabhsingh
Kunal Chopra
Greedy Algorithms | Set 5 (Prim’s Minimum Spanning Tree (MST)) · 9 hours ago
6 of 6 08/03/2014 00:09