I am a college student majoring in Artificial Intelligence. I feel stupid because I just can't understand Time Complexity. And my teacher won't help me out. Im failing and clueless. Can anyone help me to understand Time Complexity? For example what is the Big O of this code? or what is the Time Complexity for this binary tree. I got 4/50 on my recent exam so Im really desperate now
Asked
Active
Viewed 26 times
0
-
1This page is about solving specific code problems. There are plenty of pages on the web who explain the Big O notation. Please consider a web search. – André Reichelt Mar 04 '20 at 10:45
-
1Does this answer your question? [What is a plain English explanation of "Big O" notation?](https://stackoverflow.com/questions/487258/what-is-a-plain-english-explanation-of-big-o-notation) – kaya3 Mar 04 '20 at 10:50
-
Thank you so much! Im new to stack overflow so I dont really know where to ask questions sorry about that – KurisuTina Mar 05 '20 at 15:08
1 Answers
0
Big-O is basically the amount of time/space (time complexity / space complexity) that a specific algorithm runs in.
It depends on the operations of the algorithm. I will exemplify the time complexity.
E.g. the function:
## l is a list of size=n
def linear(l):
for i in l:
print(i)
iterates over a list of size=n, so the algorithm will will need to get all the n values until accomplished its purpose.
So, it's big-o is O(n) as it runs linearly with the amount of n.
The function:
## l is a list of k lists, each of them has size=n
def quadratic(matrix):
for l in matrix:
for i in l:
print(i)
Here, you have to iterate over k lists, and over n elements in each of them.
So, the algorithm runs in O(k*n).

samthegolden
- 1,366
- 1
- 10
- 26