This week we talked about big-o, and preview methods for recursion sorting. Truth be told I welcome the challenge of sorting recursion, but the mathematical big-o definition got me confused for a while.
After hours of studying, I figured out the whole point is to find out about how efficient is an algorithm given input size of n, and we measure time complexity in the order of number of operations an algorithm uses in its worst-case and will demonstrate it using big_O. It ranges from O(n) to O(n^2), or even O(n^n), the bigger the O, the less efficient the code is. The key factor of efficiency in a code depends on how many loops, and functions are called. The trick I found, is to number how many "for","while" loops are there, and do the calculation accordingly.
One thing about big-o that really worth mentioning is that a trick about "exponential calculation". for example the following code :
def silly2(n):
n = 17 * n**2 n = n + 3
print("n is: {}.".format(n))
if n > 1997: for i in range(n): print(‘so big!‘)
elif n > 97: print(‘big!')
else: print('not so big!')
It's easy to get confused by its n**2n calculation. In fact, the big-O for this function is only 1. Doesn't really matter how complicated is the calculation for n, as long as it's not a loop, nor a function called, it only runs once.
No comments:
Post a Comment