Download presentation
Presentation is loading. Please wait.
Published bySusanna Watts Modified over 9 years ago
1
Algorithm Evaluation
2
What’s an algorithm? a clearly specified set of simple instructions to be followed to solve a problem a way of doing something What are some different search algorithms? What are some different sorting algorithms?
3
How good is it? Measure performance by measuring resource usage time space Space should be consistent across different machines, but what about time? What if you ran an algorithm on both a Intel Pentium II and an Intel Core i7?
4
How many comparisons? Assume an array containing n=10 elements: 4, 2, 62, 6, 78, 8, 5, 91, 63, 13 Using the linear search algorithm: How many comparisons would we need to find something at the beginning (e.g., 4)? On average how many comparisons would we need to find something in the middle? How many comparisons would we need to find something at the end (e.g., 13)?
5
Linear Search Evaluation Assuming an array of n elements, linear search yields: Best case: 1 comparison Average case: n/2 comparisons Worst case: n comparisons
6
Big O! a mathematical representation of the complexity of an algorithm “on the order of...” usually used to estimate the upper bound performance O( # comparisons ) or O( # operations ) or O( # of times things happen)
7
Summary of Complexities O(1) : constant time not related to length of data set O(log 2 n) : log time and is fast O(n) : linear time O(n log 2 n) : “n log n” time O(n 2 ) : quadratic time, polynomial complexity O(n 3 ) : cubic time, polynomial complexity O(n n ) : not computable
8
A Simple Example k = 0 i = 0 while i < n: k += 1 n += 1 What’s my complexity?!? (Hint: use Big O notation)...on the order of...
9
A Simpler Example k = 0 k+=1 What’s my complexity?!? (Hint: use Big O notation)...on the order of...
10
Another Example for i in range(n): for j in range(n): for k in range(n): a++; What’s my complexity?!? (Hint: use Big O notation)...on the order of...
11
::sigh:: Another Example k = 0 for i in range(n): for x in range(n): k += 1 What’s my complexity?!? (Hint: use Big O notation)...on the order of...
12
Rules of Thumb one loop O(n) two nested loops O(n 2 ) three nested loops O(n 3 )
13
Back to linear search... Best case: 1 comparison O(1) Worst case: n comparisons O(n) Average case: n/2 comparisons...wait...what if n is really, really, really big? O(n/2) reduces to O(n) Linear search’s complexity is O(n)
14
Complexity Reduction Rules of thumb: consider LARGE values of n removes constants larger complexities overshadow smaller ones removes smaller Os Examples: O(n/2) O(n) O(2*n) O(n) O(n + 5) O(n) O(n + 209502) O(n) O(n 2 ) + O(n) O(n 2 + n) O(n 2 )
15
Reduction Example k = 0 for i in range(n/2): k += 1 Notice the “n/2”! What’s the complexity?
16
Binary Search Algorithm Assume a sorted array containing n=10 elements: 2, 4, 5, 6, 8, 13, 62, 63, 78, 91 Using the binary search algorithm: What is the best case performance? What is the worst case performance? What is the O complexity?
17
Summary of Common Complexities linear search : O(n) binary search : O(log 2 n) bubble sort : O(n 2 ) selection sort : O(n 2 ) quick sort : O(n log 2 n)
18
IB Expectations Evaluate the efficiency of a given algorithm How many times does it run exactly? How do you improve the algorithm? Trace an algorithm (follow its steps) when given input Analyze an algorithm as pseudocode or flowchart
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.