Computer Science to describe the performance or complexity of an algorithm. ▪ It specifically gives an indication of how well an algorithm scales, it is not a measure of efficiency. ▪ Big O specifically describes the worst-case scenario, and can be used to describe the execution time required or the space used (e.g. in memory or on disk) by an algorithm
a look at the following equation to get an idea of how algorithms can grow as the data set increases: 45n3 + 20n2 + 19 = ? What is the answer when n = 1?
19 = 47,019 When n increases from 2 to 10, both the 19 and the 20n2 become less significant. ▪ The real contributory factor is 45n3 = 45,000 ▪ Algorithm is said to be order n3 or O (n3)
to worst are some typical notations: – O(1) – O(log n) – O(n) – O(n log n) – O(n2) ▪ Let’s look at some examples in python to observe the difference in time taken to complete algorithms.
n means, the exponent you would need to raise the number 2 to to get n. ▪ So imagine, if n = 16. Our exponent would be much much smaller than the actual n value. ▪ It would be 4.
each step you effectively half your problem. If you double the problem size n, your algorithm needs only a constant number of steps more. ▪ O(log n) code
grow linearly and in direct proportion to the size of the input data set. ▪ Big O notation will always assume the upper limit where the algorithm will perform the maximum number of iterations. ▪ O(n) code
performance is directly proportional to the square of the size of the input data set. ▪ This is common with algorithms that involve nested iterations over the data set. Deeper nested iterations will result in O(n3), O(n4) etc. ▪ O(n2) code
programs can be analysed by counting the nested loops of the program. ▪ A single loop over n items yields f(n) = n ▪ A loop within a loop yields f(n) = n2 ▪ A loop within a loop within a loop yields f(n) = n3
you have nested loops, and the outer loop iterates i times and the inner loop iterates j times, the statements inside the inner loop will execute a total of i x j times. ▪ This is because the inner loop will iterate j times for each of the i iterations of the outer loop. ▪ This means that if both the outer and inner loop are dependent on the p roblem size n, the statements in the inner loop will be executed O(n2) times.