r/learnprogramming • u/Temporary-Warthog250 • Apr 05 '22
Name one programming/comp sci concept you never understood and if you understand it, try to explain it to them
Name a programming concept such as virtual functions, pointers, certain algorithms, etc that you always had a hard time understanding and why.
Someone else who reads yours and does understand it, try to explain it to them to help them learn
1.6k
Upvotes
166
u/tzaeru Apr 05 '22 edited Apr 05 '22
Big O basically tells how much time or space a function needs when its input grows in size.
For example, consider this algorithm:
If
nis 1, what's the number returned? It's 1*1 = 1. There was 1 increment operation done.If
nis 4, what's the number returned? It's 4*4 = 16. There were 16 increment operations done.So you see the algorithm's time complexity grows by n^ 2. Big O notation is a way of expressing this; we can say that the time complexity is O(n2 ).
You can simply get the time complexity by counting the number of operations in the function and comparing how that number changes depending on the size of the input.
O(log n) is one very common time complexity and one example of an algorithm with O(log n) time complexity is searching a sorted array with binary search. The reason it's log n is that in each step, binary search splits the search space in two; so with input with size of 8 there are at most 4->2->1 or 3 steps, while with input size of 16 there's at most 8->4->2->1 or 4 steps.
4/3 = 1.33.. and log(16)/log(8) = 1.33..