Download presentation
Presentation is loading. Please wait.
1
Big-O Analysis Example
2
Big O & Functions Calculating work: Normal line of code : 1
Loop : repetitions * work inside loop Can apply BigO simplification to both parts Function : work done inside function Can apply BigO simplification
3
Big O Describes Categories
3 units of work = O(1) int x = 2; //1 int y = x + 10; //1 double z = 1.5 * x + y * sqrt(y); //1
4
Big O Describes Categories
n * 1 unit of work = O(n) int x = 2; //1 int y = x + 10; //1 double z = 1.5 * x + y * sqrt(y); //1
5
Example Work for this function: f(n) = (n-1) = 4n – 1 = O(n)
6
Example Simplify as we go… Inside loop = 4 = O(1)
N-1 loops that do 1 work= O(n * 1) = O(n) +3 = O(n + 3) = O(n)
7
Loops Loops driving factor of Big-O
Pattern of loop counter tells repetitions Loop Pattern Repetitions for(int i = 0; i < n; i++) … n – 1 n for(int i = 0; i <= n; i++) … n – 1 n n + 1 = n for(int i = 0; i < n; i += 2) … n – 1 n / 2 = n for(int i = 1; i < 2*n; i++) … 2n 2n = n for(int i = 1; i < n; i *= 2) … logn
8
Logs Log patterns Loop counter multiplied
… for(int i = 1; i < n; i = i * 2) Remaining work divided at each step … counter = while(counter > 0) { counter /= 2; }
9
Log Fun Logs of differing base differ only by constant factor:
Base often unspecified in Big O Generally 2 n log2(n) log10(n) 10 3.322 1 100 6.644 2 1000 9.966 3 10000 13.288 4
10
Nested Loops Nested loops get multiplied: n2 nlogn Loop Pattern
Repetitions for(int i = 0; i < n; i++) for(int j = 0; j < n; j++) i = j = … n – 1 i = j = … n – 1 … i = n j = … n – 1 n * n n2 for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j = j * 2) i = j = … n i = j = … n i = j = … n i = j = … n … i = n j = … n n * log2n nlogn
11
Tricky Nested Loop What is BigO for these loops? Loop Pattern Work
for(int i = 0; i < n; i++) for(int j = 0; j <= i; j++) i = j = 0 i = j = 0 1 i = j = i = j = … i = n – 1 j = … n – 1 1 2 3 4 … n
12
Tricky Nested Loop Sum of work 1+2+3+…+ 𝑛−3 + 𝑛−2 + (𝑛−1)
1+2+3+…+ 𝑛−3 + 𝑛−2 + 𝑛−1
13
( 𝑛−1 2 𝑔𝑟𝑜𝑢𝑝𝑠)(𝑛 𝑝𝑒𝑟 𝑔𝑟𝑜𝑢𝑝)= 𝑛−1 2 𝑛 = 𝑛 2 −𝑛 2
Increasing Work Sum of work: 1+2+3+…+ 𝑛−3 + 𝑛−2 + (𝑛−1) 1+2+3+…+ 𝑛−3 + 𝑛−2 + 𝑛−1 ( 𝑛−1 2 𝑔𝑟𝑜𝑢𝑝𝑠)(𝑛 𝑝𝑒𝑟 𝑔𝑟𝑜𝑢𝑝)= 𝑛−1 2 𝑛 = 𝑛 2 −𝑛 2 𝑛 2
14
Tricky Nested Loop What is BigO for these loops? n2 Loop Pattern
Repetitions for(int i = 0; i < n; i++) for(int j = 0; j < n; j++) i = j = … n – 1 i = j = … n – 1 i = j = … n – 1 … i = n j = … n – 1 n * n n2 for(int i = 0; i < n; i++) for(int j = 0; j <= i; j++) i = j = 0 i = j = 0 1 i = j = i = j = … i = n – 1 j = … n – 1 n2 / 2
15
Full Analysis What is Big-O?
16
Big O & Functions What is n?
17
Big O & Functions N = size of array
18
Big O & Functions Declaring variable = constant
Initializing n values = O(n)
19
Print printArray = Loop + O(1)
Loop repeats n times, has 1 unit of work inside
20
Print printArray = Loop + O(1) = O(n * 1) = O(n)
21
So Far So far: 3n + 1 = O(N)
22
Sort Loops N time
23
Sort Assignments are all O(1)
24
Sort Total work in loop = 4* O(1) + find Location
25
Find findLocation = do constant work N times O(n)
26
Sort Total work in loop = 4*O(1) + O(n) = O(1) + O(n) = O(n)
27
Sort While loop repeats n times Does O(n) work Sort = O(n * n) = O(n2)
28
Main 3N N2 = O(N2) Total work of program O(n2)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.