Time complexity question: why is it O(n log n)?
Question:
Can someone explain why this is O(n log n)
and not O(n^2)
? I had the idea that the if
statement is n
times and the else
is log n
so you pick the worst case between the two in this case is O(n)
so multiply it with the outer loop O(n)
making it O(n^2)
but apparently is O(n log n)
and I am not seeing how.
for i in range( len(nums_lst)):
if i < 10:
for k in range( len(nums_lst)):
print(nums_lst[0])
else:
j = 1
while j < len(nums_lst):
print(nums_lst[0])
j *= 2
Answers:
For a large value of n, the if:
condition will be neglected.
Inside else:
, while loop continues to run for –
j = 1 and j = 2 and j = 4 and … and j = 2^m where 2^m <= n
So the total number of loops is m+1. And m = log n
Now the time complexity is O(n * log n)
for i in range( len(nums_lst)): # Time complexity O(n)
if i < 10:
...
else:
j = 1
while j < len(nums_lst): # Time complexity O(logn)
print(nums_lst[0])
j *= 2
Big-O time complexity is an asymptotic measure of how an algorithm scales. It is concerned with how the algorithm scales as n
approaches infinity, rather than for specific values of n
.
When nums_list
contains fewer than ten items, the complexity is O(n^2) but this is a negligible sample as nums_list
scales in size.
The much larger sample will be represented by the else branch where j
approaches len(nums_list)
exponentially. Thus the complexity of the inner loop is O(log n).
Consequently, multiplying the two, we get O(n*log n).
It’s easier to see what’s going on if you rewrite it this way, which is equivalent when n ≥ 10:
for i in range(10):
for k in range(len(nums_lst)):
print(nums_lst[0])
for i in range(10, len(nums_lst)):
j = 1
while j < len(nums_lst):
print(nums_lst[0])
j *= 2
The number of steps is 10n + (n − 10) log n.
Asymptotic notations are always the measure of performance for larger inputs.
so if you are going to calculate Time Complexity go for Big-O (i.e) Worst case .
Since i< 10 is small we can neglect and take else part whose time complexity is O(log n to the base 2) and Outer for loop have T.C of O(n).
Hence O(n log n).
This algorithm will be O(nlog(n)) because the chances of the execution of if block is very low because in the most cases the list will have more than 10 elements. Only in few worse cases the list will have less than 10 elements and the time complexity will be O(n^2). So, overall the algorithm will have a time complexity of O(nlog(n)).
Can someone explain why this is O(n log n)
and not O(n^2)
? I had the idea that the if
statement is n
times and the else
is log n
so you pick the worst case between the two in this case is O(n)
so multiply it with the outer loop O(n)
making it O(n^2)
but apparently is O(n log n)
and I am not seeing how.
for i in range( len(nums_lst)):
if i < 10:
for k in range( len(nums_lst)):
print(nums_lst[0])
else:
j = 1
while j < len(nums_lst):
print(nums_lst[0])
j *= 2
For a large value of n, the if:
condition will be neglected.
Inside else:
, while loop continues to run for –
j = 1 and j = 2 and j = 4 and … and j = 2^m where 2^m <= n
So the total number of loops is m+1. And m = log n
Now the time complexity is O(n * log n)
for i in range( len(nums_lst)): # Time complexity O(n)
if i < 10:
...
else:
j = 1
while j < len(nums_lst): # Time complexity O(logn)
print(nums_lst[0])
j *= 2
Big-O time complexity is an asymptotic measure of how an algorithm scales. It is concerned with how the algorithm scales as n
approaches infinity, rather than for specific values of n
.
When nums_list
contains fewer than ten items, the complexity is O(n^2) but this is a negligible sample as nums_list
scales in size.
The much larger sample will be represented by the else branch where j
approaches len(nums_list)
exponentially. Thus the complexity of the inner loop is O(log n).
Consequently, multiplying the two, we get O(n*log n).
It’s easier to see what’s going on if you rewrite it this way, which is equivalent when n ≥ 10:
for i in range(10):
for k in range(len(nums_lst)):
print(nums_lst[0])
for i in range(10, len(nums_lst)):
j = 1
while j < len(nums_lst):
print(nums_lst[0])
j *= 2
The number of steps is 10n + (n − 10) log n.
Asymptotic notations are always the measure of performance for larger inputs.
so if you are going to calculate Time Complexity go for Big-O (i.e) Worst case .
Since i< 10 is small we can neglect and take else part whose time complexity is O(log n to the base 2) and Outer for loop have T.C of O(n).
Hence O(n log n).
This algorithm will be O(nlog(n)) because the chances of the execution of if block is very low because in the most cases the list will have more than 10 elements. Only in few worse cases the list will have less than 10 elements and the time complexity will be O(n^2). So, overall the algorithm will have a time complexity of O(nlog(n)).