[Javascript Data Structure and Algorithms] Exercise 1. Big-O Notation
Exercises
Calculate the time complexities for each of the exercise code snippets
Exercise 1
Exercise 2
Exercise 3
Exercise 4
Exercise 5
Exercise 5
Answer
- O(n²)
There are two nested loops. Ignore the constants in front of n. - O(n³)
There are four nested loops, but the last loop runs only until 10. - O(1)
Constant complexity. The function runs from 0 to 1000. This does not depend on n. - O(n)
Linear complexity. The function runs from 0 to 10n. Constants are ignored in Big-O - O(log₂n)
Logarithmic complexity. For a given n, this will operate only log₂n times because i is incremented by multiplying by 2 rather than adding 1 as in the other examples. - O(∞)
Infinite loop. This function will not end