1 / 11

Big O

Big O. Let’s hope this doesn’t take too long. 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

sarah
Download Presentation

Big O

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  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(inti2 = 0; i2 < arr.length; i2++){ //code } } } The method above would have a Big O of n2 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 loopwith 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 loopwith 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 loopwith 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 n2. The first set of nested for loops have a Big O of n2. The second has a Big O of n. We could describe the Big O as n2 + 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, inti){ 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, inti){ if(i >= arr.length) return; //code algorigthm(arr, i + 1); algorigthm(arr, i + 1); } The method above would have a Big O of 2n. When we make multiple recursive calls we have a Big O of xn, 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.

More Related