Download presentation
Presentation is loading. Please wait.
1
CS 280 Data Structures Professor John Peterson
2
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?
3
Example public void r(int i) { if (i == 0) return; System.out.print(i) r(i-1); r(i-1); } r(n)
4
Example: 2 N public void r(int i) { if (i == 0) return; System.out.print(i) r(i-1); r(i-1); } r(n)
5
Dominating Terms The big idea behind Big-O notation is that when you add complexities, one term may dominate the other. That is, once n has reached some value, one complexity is ALWAYS bigger than another. Example: O(n 2 ) + O(n) = O(n 2 + n) = O(n 2 )
6
A Complexity Ladder O(1) O(log(n)) O(n) O(n log(n)) O(n 2 ) O(2 n ) There are lots more complexities “between the cracks” but these are the ones that we’ll be seeing this term.
7
Insertion Sort Complexity What is the complexity of insertion sort? insertionSort(array A) for i <- 1 to length[A]-1 do value <- A[i] j <- i-1 while j >= 0 and A[j] > value do A[j + 1] = A[j]; j <- j-1 A[j+1] <- value
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.