Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 280 Data Structures Professor John Peterson. Big O Notation We use a mathematical notation called “Big O” to talk about the performance of an algorithm.

Similar presentations


Presentation on theme: "CS 280 Data Structures Professor John Peterson. Big O Notation We use a mathematical notation called “Big O” to talk about the performance of an algorithm."— Presentation transcript:

1 CS 280 Data Structures Professor John Peterson

2 Big O Notation We use a mathematical notation called “Big O” to talk about the performance of an algorithm. O(n 2 ) means that an algorithm runs in time proportional to n 2 (the “size” of the input squared). Note that there is no constant – it’s not 23n 2 or something like that – this compares general algorithms to each other. Constant factors are hard to measure and understand!

3 Program Example Consider the following program: for (int i = 0; i < n; i++) { System.out.print(i);} This program takes O(n) time to execute. This assumes that all of the statements in the program execute in constant time. (Do they?)

4 Iteration This is a special case of a general idea: the complexity of code within a loop is calculated by multiplying the number of times the loop is executed by the complexity of the loop body. What is the complexity of this: for (int i = 0; i < n*n; i++) System.out.print(i);

5 Nesting How about this? for (int i = 0; i < n; i++) for (int j = 0; j < n, j++) for (int k = 0; k < n; k++) System.out.println(i+j+k);

6 Conditionals The if statement is trickier – you need to choose the greater complexity of the alternatives. Sometimes you can use extra information to avoid always assuming the worst. While loops are related – you need to assume the worst in general.

7 If for (int i = 0; i < n; i++) { if (a[i] == 0) { for (int j = 0; j < n; j++) { System.out.print (i + “ “ + j); }}} What is the input to this one? What does the complexity really depend on?

8 Best, Worst, and Average When we don’t know what will happen with an “if”, we can make a number of assumptions: The worst possible outcome (highest complexity) The best possible outcome (lowest complexity) An average between the best and worst – this often hard to describe

9 A Simple Method boolean find(int [] a, int v) { for (int i = 0; i < a.length; i++) { if (a[i] == v) return true;} return false; } What is “n” in this case? What is the best case complexity? What is the worst case? Average?

10 Method Calls What do you do about a method call? You need to know how complex the method is GIVEN THE PARAMETER VALUES. This can be really tricky! The worst case is always an option if nothing is known about the parameters.

11 Example: log(N) This is where things get hairy! How would you compute Log 10 (N) in a very approximate manner? What does “Log(N)” mean mathematically? How might we get this in a real piece of code? Why don’t we really need to know the base of the logarithm?

12 Log Complexity int j = n; while (j > 0) { System.out.println(j); j = j / 2; /* Integer division! */ } What would this print for n = 10? 100?


Download ppt "CS 280 Data Structures Professor John Peterson. Big O Notation We use a mathematical notation called “Big O” to talk about the performance of an algorithm."

Similar presentations


Ads by Google