Program Efficiency - PowerPoint PPT Presentation

About This Presentation
Title:

Program Efficiency

Description:

So, (n - 1) (n-2) ... 1 = n(n-1)/2, or O(n2) Program Efficiency. Big-O notation ... n n n 'work', so this is O(n3) Big-O Example 5. for(int i = 0; i = n; i ) ... – PowerPoint PPT presentation

Number of Views:18
Avg rating:3.0/5.0
Slides: 10
Provided by: marks9
Learn more at: http://www.cs.sjsu.edu
Category:

less

Transcript and Presenter's Notes

Title: Program Efficiency


1
Program Efficiency
  • Interested in order of magnitude
  • That is, how the work grows as n grows
  • Where n is the problem size
  • Usually we just look at the loops
  • Since thats where most work occurs
  • Big-O notation
  • O(f(n)) means work grows like about f(n)
  • Constants dont matter

2
Big-O Example 1
  • for(int i 0 i lt x.length i)
  • no loops here
  • We say this is O(n)
  • Since it takes about x.length work
  • Depends on problem size
  • Size is always given as n

3
Big-O Example 2
  • for(int i 0 i lt x.length i)
  • for(int j 0 j lt x.length j)
  • no loops here
  • Its n work for each of n times thru outer loop
  • So, this is O(n2)

4
Big-O Example 3
  • for(int i 0 i lt x.length i)
  • for(int j i 1 j lt x.length j)
  • no loops here
  • When i 0, we have n - 1 work
  • When i 1, we have n - 2 work, etc.
  • So, (n - 1)(n-2) 1 n(n-1)/2, or O(n2)

5
Program Efficiency
  • Big-O notation
  • O(1) constant time
  • O(n) linear time
  • O(n log n) log linear
  • O(n2) quadratic
  • O(n3) cubic
  • O(2n) exponential
  • O(n!) factorial
  • Polynomials are good, exponentiala are bad

6
Big-O Example 4
  • for(int i 1 i lt n i)
  • for(int j 1 j lt n j)
  • for(int k n k gt 1 k--)
  • no loops here
  • n ? n ?n work, so this is O(n3)

7
Big-O Example 5
  • for(int i 0 i lt n i)
  • for(int j 0 j lt i i j)
  • no loops here
  • 02 12 22 n2 n(n1)(2n1)/6
  • So, O(n3)

8
Big-O Example 6
  • for(int i n i gt 0 i - 2)
  • no loops here
  • About n/2 iterations
  • So, O(n)

9
Big-O Example 7
  • for(int i 0 i lt n i)
  • for(int j i j gt 0 j / 2)
  • no loops here
  • 0 1 2 2 3 3 4 4 log n log n
  • Adds up to about (log n)((log n) 1)
  • Where log is to the base 2
  • This is O((log n)2)
Write a Comment
User Comments (0)
About PowerShow.com