Download presentation

Presentation is loading. Please wait.

Published byKarlie Wicken Modified over 2 years ago

1
Let’s hope this doesn’t take too long. BIG O

2
WHY WE SHOULD CARE Computers are can perform billions of operations per second but they are still not infinite in their power. Each operation, while seemingly instantaneous still takes time and resources If we are not careful in the way we design our algorithms, they can become inefficient Their run time could even outlast our own lifetimes If Usain Bolt and Mr. Mayewsky were in a race, Mr. Mayewsky would win if all he has to do is walk around the school while Usain has to run around the earth.

3
BIG O FOR A FOR LOOP public void algorithm(int [] arr){ for(int i1 = 0; i1 < arr.length; i1++){ //code } The method above would have a Big O of n where n is the size of what ever array gets passed in. We don’t know the size beforehand but we do know that it will directly correspond to the size of the array

4
BIG O FOR A NESTED FOR LOOP public void algorithm(int [] arr){ for(int i1 = 0; i1 < arr.length; i1++){ for(int i2 = 0; i2 < arr.length; i2++){ //code } The method above would have a Big O of n 2 where n is the size of what ever array gets passed in. The inside loop will have to go through all instances of n for every one time that the outside loop executes. Think of it like planting a row of crops and then being told you have multiple rows to plant. For each nested loop, Big O would be multiplied by a factor of n.

5
BIG O FOR A FOR LOOP WITH A STATIC END public void algorithm(int [] arr){ for(int i1 = 0; i1 < 1000000000; i1++){ //code } The method above would have a Big O of only 1. While the for loop is going to execute one billion times, it is no longer dependent on the size of the array. So it will always be limited at one billion

6
BIG O FOR A FOR LOOP WITH AN EXPONENTIAL DECREASE public void algorithm(int [] arr){ for(int i1 = 0; i1 < arr.length; i1 *= 2){ //code } The method above would have a Big O of only log(n). The loop is still dependent on the number of elements in the array, but the index is increasing exponentially since we have i1 *= 2 instead of i1++.

7
BIG O FOR A FOR LOOP WITH AN MULTIPLIER public void algorithm(int [] arr){ for(int i1 = 0; i1 < 2 * arr.length; i1++){ //code } The method above would have a Big O of n. Notice that the length of the array is being multiplied by a factor of two so you could describe the Big O as 2n. But in the world of Big O we don’t care about static multipliers.

8
BIG O FOR SEQUENTIAL FOR LOOPS public void algorithm(int [] arr){ for(int i1 = 0; i1 < arr.length; i1++){ for(int i2 = 0; i2 < arr.length; i2++){ //code } for(int i1 = 0; i1 < arr.length; i1 += 1){ //code } The method above would have a Big O of n 2. The first set of nested for loops have a Big O of n 2. The second has a Big O of n. We could describe the Big O as n 2 + n but we only pay attention to the most sever component. Think of it this way, you might be concerned if there was a scorpion crawling around you feet but not if there is Godzilla standing right behind you.

9
BIG O FOR A SINGLE CALL RECURSION public void algorithm(int [] arr, int i){ if(i >= arr.length) return; //code algorigthm(arr, i + 1); } The method above would have a Big O of n. When we only make one recursive call, recursion acts in the same way as a for loop.

10
BIG O FOR MULTIPLE CALL RECURSION public void algorithm(int [] arr, int i){ if(i >= arr.length) return; //code algorigthm(arr, i + 1); } The method above would have a Big O of 2 n. When we make multiple recursive calls we have a Big O of x n, we have branching take place, where n is the size of the array and the depth that each branch reached and x is the number of branching that take place at each level.

11
BEST, WORST, AND AVERAGE CASE The analysis of Big-O can be a complex science and we are only getting a basic look at it. What we have looked at are a lot of general cases. Depending on the algorithm, the Big-O can be difficult to analyze. Most also have some type of Best, Worst, and Average Case depending on the conditions of the input. Think of it like mowing a lawn. If you go to mow a lawn, it might have been 2 days ago, might have been mowed 2 weeks ago, or it might have been mowed 2 years ago.

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google