Question
Find the time complexity of the following algorithms in terms of Big O. Explain your answer as well. A function (or set of statements) that
Find the time complexity of the following algorithms in terms of Big O. Explain your answer as well.
- A function (or set of statements) that doesnt contain loop, recursion and call to any other non-constant time function.
- A loop or recursion that runs a constant number of times, such as follows:
for (int i = 1; i <= c; i++) // Here c is a constant
{
// some O(1) expressions
}
- A loop where the loop variables is incremented / decremented by a constant amount, as follows:
// Here c is a positive integer constant
for (int i = 1; i <= n; i += c) {
// some O(1) expressions
}
- Time complexity of nested loops as follows:
// Here c is a positive integer constant
for (int i = 1; i <=n; i += c) {
for (int j = 1; j <=n; j += c) {
// some O(1) expressions
}
}
- Time Complexity of a loop if the loop variables is divided / multiplied by a constant amount as follows:
for (int i = 1; i <=n; i *= c) {
// some O(1) expressions
}
- Time Complexity of a loop if the loop variables is reduced / increased exponentially by a constant amount as follows:
// 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
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started