What is Big O notation? Do you use it?
I missed this university class I guess :D
Does anyone use it and give some real life examples of where they used it?
Big-O for Eight Year Olds?
Big O, how do you calculate/approximate it?
Did you apply computational complexity theory in real life?
With Big O notation, we use the size of the input, which we call " n." So we can say things like the runtime grows "on the order of the size of the input" ( O ( n ) O(n) O(n)) or "on the order of the square of the size of the input" ( O ( n 2 ) O(n^2) O(n2)).
Big O Notation is a tool used to describe the time complexity of algorithms. It calculates the time taken to run an algorithm as the input grows. In other words, it calculates the worst-case time complexity of an algorithm. Big O Notation in Data Structure describes the upper bound of an algorithm's runtime.
Big O notation ignores constants. For example, if you have a function that has a running time of 5n, we say that this function runs on the order of the big O of N. This is because as N gets large, the 5 no longer matters.
One important thing most people forget when talking about Big-O, thus I feel the need to mention that:
You cannot use Big-O to compare the speed of two algorithms. Big-O only says how much slower an algorithm will get (approximately) if you double the number of items processed, or how much faster it will get if you cut the number in half.
However, if you have two entirely different algorithms and one (A
) is O(n^2)
and the other one (B
) is O(log n)
, it is not said that A
is slower than B
. Actually, with 100 items, A
might be ten times faster than B
. It only says that with 200 items, A
will grow slower by the factor n^2
and B
will grow slower by the factor log n
. So, if you benchmark both and you know how much time A
takes to process 100 items, and how much time B
needs for the same 100 items, and A
is faster than B
, you can calculate at what amount of items B
will overtake A
in speed (as the speed of B
decreases much slower than the one of A
, it will overtake A
sooner or later—this is for sure).
Big O notation denotes the limiting factor of an algorithm. Its a simplified expression of how run time of an algorithm scales with relation to the input.
For example (in Java):
/** Takes an array of strings and concatenates them * This is a silly way of doing things but it gets the * point across hopefully * @param strings the array of strings to concatenate * @returns a string that is a result of the concatenation of all the strings * in the array */ public static String badConcat(String[] Strings){ String totalString = ""; for(String s : strings) { for(int i = 0; i < s.length(); i++){ totalString += s.charAt(i); } } return totalString; }
Now think about what this is actually doing. It is going through every character of input and adding them together. This seems straightforward. The problem is that String is immutable. So every time you add a letter onto the string you have to create a new String. To do this you have to copy the values from the old string into the new string and add the new character.
This means you will be copying the first letter n times where n is the number of characters in the input. You will be copying the character n-1
times, so in total there will be (n-1)(n/2)
copies.
This is (n^2-n)/2
and for Big O notation we use only the highest magnitude factor (usually) and drop any constants that are multiplied by it and we end up with O(n^2)
.
Using something like a StringBuilder
will be along the lines of O(nLog(n)). If you calculate the number of characters at the beginning and set the capacity of the StringBuilder
you can get it to be O(n)
.
So if we had 1000 characters of input, the first example would perform roughly a million operations, StringBuilder
would perform 10,000, and the StringBuilder
with setCapacity
would perform 1000 operations to do the same thing. This is rough estimate, but O(n)
notation is about orders of magnitudes, not exact runtime.
It's not something I use per say on a regular basis. It is, however, constantly in the back of my mind when trying to figure out the best algorithm for doing something.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With