Big O notation and more recursion
- rexchao
- Apr 13, 2020
- 2 min read
Today I looked up some information on the time complexity of a program, usually represented using the Big O notation. This shows how much time a program would need to spend as the input size grows.
For example, O(1) means that this function will always run execute in the same time (or space) regardless of the size of the input data set. This is also called constant space-time. A program with O(1) will most likely run instantly. O(n) describes an algorithm where the time needed to run is proportional to the size of the input data. A program that searches a number from a list of numbers by iterating through the list will have a time complexity of O(n) in a worst case scenario. There is also time complexity of O(n^2). An example of this will be a for loop nested inside another for loop. This will make the program run the input number ^2 times. You can see how this would grow very fast soon, so its not the best if we make a program that requires higher time-complexity, unless it is very hard to implement in linear or constant time. The worst is O(2^n) or O(n^n) or exponential time complexity. The run time would take exponentially longer when the input size increases. An example of this is the recursive implementation of the Fibonacci series in a program. (Obvious there's much better ways to write it)
I found a super nice picture demonstrating the graph representation of each time-complexity. It is very obvious that an exponential time-complexity would scale much much faster than a linear or constant one.

I also learned about several search and sorting algorithms such as binary search and quicksort. I tried to implement a binary search program in python and I think this is the first program where writing a recursive program is easier than writing an iterative one. An explanation of what binary search and sorting are and the solution of how to implement one will come out tomorrow (probably). I've successfully written it using recursion, but I haven't started writing the iterative one, but I have a bit of an idea on how to implement it. Hopefully, I can finish it tomorrow, so wait for it.
-Rex
Comments