Title: Lecture 24 Final Review
1Lecture 24 Final Review
- Comp 208 Computers in Engineering
- Yi Lin
- Winter, 2007
2Final 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
3Algorithms
- 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)
4Big 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)
5Big 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.
6Big O Examples
- Calculate the Big O for the following algorithm
- y0
- for (x 0 x lt N x)
- y y x 10
7Big 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)
8Big 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)
9Big 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)
10SearchingLinear, Binary
- Comp 208 Computers in Engineering
- Yi Lin
- Fall, 2005
11Linear 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
12Binary 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
-
13Pros 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
14Sorting selection, bubble, insertion, merge
- Comp 208 Computers in Engineering
- Yi Lin
- Fall, 2005
15Motivation
- 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
16Selection sort
First iteration
2nd iteration
3rd iteration
4th iteration
17Bubble sort
18Bubble sort
19Two 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
20Insertion sort
21Insertion sort
22Insertion sort
23Introduction 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.
24Example 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
-
25Recursive
Factorial(3)
3 Factorial(2)
2 Factorial(1)
1 Factorial(0)
1 1
2 1
3 2
6
Exe flow
26Introduction 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
27Introduction 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
28Example How to merge sort
29Root finding bisection
30Root Finding Secant
31Root 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.
32Root Finding NewtonRaphson
- Much like secant, but using the real derivative
33Integration
- Midpoint
- Trapezoid
- Simpson
- Monte-Carlo
34Differentiation 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