Lecture 24 Final Review - PowerPoint PPT Presentation

About This Presentation
Title:

Lecture 24 Final Review

Description:

Space (not mentioned in the class) Data memory usage (RAM & disk) Code size ... O(loge n): Next best time - Logarithmic time. O(n): Next best time - Linear time ... – PowerPoint PPT presentation

Number of Views:31
Avg rating:3.0/5.0
Slides: 35
Provided by: Yi
Category:
Tags: final | lecture | loge | review

less

Transcript and Presenter's Notes

Title: Lecture 24 Final Review


1
Lecture 24 Final Review
  • Comp 208 Computers in Engineering
  • Yi Lin
  • Winter, 2007

2
Final Review Algorithms
  • Learn about how to evaluate algorithms, O(..)
  • Non-Numerical
  • Searching
  • Sorting
  • Numerical
  • Root finding,
  • Integration
  • Solving differential equations
  • Linear algebra
  • Learn enough in-depth C to implement these

3
Algorithms
  • How to measure the performance of algorithms
  • Accuracy
  • Speed
  • O() number of operations ( - / lt gt )
  • Space (not mentioned in the class)
  • Data memory usage (RAM disk)
  • Code size (RAM disk)

4
Big O Notation
  • O(1) The best time - Constant time
  • O(loge n) Next best time - Logarithmic time
  • O(n) Next best time - Linear time
  • O(nlogen) Net best time - Multiplied
    logarithmic time
  • O(nk) Next is - Quadratic time (k is constant)
  • O(nm) The Worst Exponential time (m is
    variable)

5
Big O
  • In many cases the Big O notation may have a
    constant that modifies the order.
  • For instance
  • O(gn) or O(n h) or O(kn log n)
  • where g, h, and k are constants
  • In general we can ignore the constants.

6
Big O Examples
  • Calculate the Big O for the following algorithm
  • y0
  • for (x 0 x lt N x)
  • y y x 10

7
Big O Examples
  • Calculate the Big O for the following algorithm
  • for(i 2 i lt N i 2)
  • Ai i
  • Calculate the Big O for the following algorithm
  • for(i N i gt 0 i / 2)
  • Ai i

The order is O(logN)
8
Big O Examples
  • Calculate the Big O for the following algorithm
  • for(i 0 i lt N i)
  • for(j 2 j lt N j 2) Ai ij

The order is O(N log N)
9
Big O Examples
  • Calculate the Big O for the following algorithm
  • z0
  • for (x 0 x lt N x)
  • for (y 0 y lt M y)
  • z z x y

The order is O(NM)
If MN, the order is O(N2)
10
SearchingLinear, Binary
  • Comp 208 Computers in Engineering
  • Yi Lin
  • Fall, 2005

11
Linear searching function
  • An example declaration would be
  • int find(int list, int size, int number)
  • int i
  • for(i 0i lt sizei)
  • if (listi number)
  • return i
  • return -1

To find 23
12
Binary search
  • int bfind(int list, int size, int number)
  • int left, right, middle
  • left0
  • rightsize-1
  • do
  • middle (leftright)/2
  • if(listmiddlenumber)
  • return middle
  • else if(listmiddle gt number)
  • leftmiddle
  • else // if(listmiddle lt number)
  • rightmiddle
  • while(left lt right)
  • return -1

13
Pros and cons of both methods
  • Linear search
  • Cost O(n)
  • Pro
  • Simple to write
  • Works on unsorted lists and sorted lists
  • Con
  • Needs to look at all elements
  • Binary search
  • Cost O(log n)
  • Pro
  • Much faster
  • Con
  • List must be sorted
  • More difficult to write

14
Sorting selection, bubble, insertion, merge
  • Comp 208 Computers in Engineering
  • Yi Lin
  • Fall, 2005

15
Motivation
  • The motivations for sorting are
  • Put a list of things in order such that it is
    easy for you to search something.
  • For example
  • To enable binary search

16
Selection sort
  • How many swaps in total?

First iteration
2nd iteration
3rd iteration
4th iteration
17
Bubble sort
  • First pass bubbling

18
Bubble sort
  • 2nd pass

19
Two optimizations
  • We know that after X passes the X smaller
    elements are at the beginning of the list, it
    therefore isn't useful to pass through these
    elements again, therefore pass X will have N-X
    comparisons instead of N
  • If we haven't swapped in a pass then the list is
    in order, therefore we can finish sorting

20
Insertion sort
  • First pass
  • 2nd pass

21
Insertion sort
  • 3rd pass
  • 4th pass

22
Insertion sort
  • 5th pass

23
Introduction to Recursion
  • A function that calls itself repeatedly is called
    a Recursive Function.
  • Using recursion, we split a complex problem into
    its single simplest case.
  • The recursive function only knows how to solve
    that simplest case.
  • You'll see the difference between solving a
    problem iteratively and recursively later.

24
Example 1 factorial(n)
  • unsigned int factorialRecursive(unsigned int n)
  • int i
  • if(n0) return 1
  • resultn factorialRecursive(n-1)
  • return result
  • unsigned int factorialNotRecursive(unsigned int
    n)
  • int i, result1
  • if (n0) return 1
  • for(i1 iltn i)
  • result i
  • return result

25
Recursive
Factorial(3)
3 Factorial(2)
2 Factorial(1)
1 Factorial(0)
1 1
2 1
3 2
6
Exe flow
26
Introduction to Merge Sort
  • Easiest array to sort (size1)
  • What if size2?
  • Either the array is sorted
  • Or isnt. Then swap the 2 elements

5
5
8
5
8
5
8
27
Introduction to Merge Sort
  • What if size gt 2?
  • Split them into two equal size sub-arrays
  • Sort these two sub-arrays
  • This process can be done recursively.
  • Then merge them


28
Example How to merge sort
29
Root finding bisection
30
Root Finding Secant
31
Root Finding False position
  • The secant method retains only the most recent
    estimate, so the root does not necessarily remain
    bracketed.
  • Combined with bisection in order to bracket the
    root.

32
Root Finding NewtonRaphson
  • Much like secant, but using the real derivative

33
Integration
  • Midpoint
  • Trapezoid
  • Simpson
  • Monte-Carlo

34
Differentiation Euler Method
  • Given a (x0,y0) and dy/dxf(x,y)
  • To approximate (x1, y1) in which x1 x0?x
  • To approximate (x2, y2) in which x2 x1?x
  • To approximate (xn, yn) in which xn x(n-1)?x

y
y1
y0
x0
x1
x
Write a Comment
User Comments (0)
About PowerShow.com