Big O
Big O
Big O
Big O-Notation
Best case:
Worst case:
Average case:
2. Definiteness: The steps of the algorithm must be precisely defined or unambiguously specified.
3. Generality: An algorithm must be generic enough to solve all problems of a particular class.
4. Effectiveness: the operations of the algorithm must be basic enough to be put down on pencil and paper. They
should not be too complex to warrant writing another algorithm for the operation.
5. Input-Output: The algorithm must have certain initial and precise inputs, and outputs that may be generated
both at its intermediate and final steps.
Complexity – Big O-Notation
Algorithm properties (2)
An algorithm does not enforce a language or mode for its expression but only demands adherence to its properties.
• 1. To save time (Time Complexity): A program that runs faster is a better program.
• 2. To save space (Space Complexity): A program that saves space over a competing program is considerable desirable.
• 1. To save time (Time Complexity): The time complexity of an algorithm or a program is a function of the running
time of the algorithm or a program. In other words, it is the amount of computer time it needs to run to completion.
The time complexity of an algorithm can be computed either by an empirical or theoretical approach. The empirical or
posteriori testing approach calls for implementing the complete algorithms and executing them on a computer for various
instances of the problem. The time taken by the execution of the programs for various instances of the problem are noted and
compared. The algorithm whose implementation yields the least time is considered as the best among the candidate algorithmic
solutions.
• 2. To save space (Space Complexity): The space complexity of an algorithm or program is a function of the space
needed by the algorithm or program to run to completion.
Meaning: For all data sets big enough (i.e., n>n0), the algorithm always
executes in less than cf(n) steps in [best, average, worst] case.
array[index]
• Accessing an array element by its index is super fast but arrays have a fixed
length and if you want to constantly add or remove items from them they
have to get resized and this will get costly as the size of our input grows
very large.
2 4
• And finally knowing Big L will make you a better developer or software
engineer.
Best case is defined for the input of size n that is cheapest among all
inputs of size n.
• This method takes an array of integers and prints the first item on a console.
• It doesn’t matter about how big is the array . We can have an array with 1 or 1
million items. All you are doing here is printing the first item.
for (int i=0; i < names.length; i++) for (int i=0; i < names.length; i++) //O(m)
System.out.println(names[i]); System.out.println(names[i]);
} }
} }
Linear Search
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
6 7 8 9 10
NB.
O(1) is the most efficient complexity for an algorithm. O(1) algorithms are a programmer’s dream, but they usually take up plenty of memory
O(2n), O(n!) : There are called exponential and factorial complexities. Ideally you should never write algorithms that perform at these
complexities since they seem to take eternity. If your measurement shows these complexities, then you have written something inefficient
and the code must be refactored.
Complexity – Big O-Notation
Time Complexity Examples (1)
Example 1: a = b;
This assignment takes constant time, so it is O(1).
Example 2:
sum = 0;
for (i=1; i<=n; i++)
sum += n;
Time: Algorithm
Space: Data Structure