90 likes | 245 Views
Algorithm Performance. CMSC 150 – Introduction to Computing. Big-Oh: Algorithm Complexity. We group algorithms into classes of similar complexity (i.e., how many steps required) Sequential search is a O( n ) algorithm (linear). O(1). O(log n ). O(n ). O(n 2 ). O(2 n ). Sequential
E N D
Algorithm Performance CMSC 150 – Introduction to Computing
Big-Oh: Algorithm Complexity • We group algorithms into classes of similar complexity (i.e., how many steps required) • Sequential search is a O(n) algorithm (linear) O(1) O(logn) O(n) O(n2) O(2n) Sequential search Modifying a 2D image Traveling Salesman (brute force) constant-time logarithmic linear quadratic exponential
Some Comparisons • nis the size of the input • Entry in table gives approximate number of steps required by an algorithm of that complexity
Analyzing binary search • Consider worst case for an array of size 32: 32 elements 16 elements 8 elements 4 elements 2 elements 1 element
Analyzing binary search • Consider worst case for an array of size 32: 32 elements 16 elements 8 elements • Ignore the last (1-elementarray) comparison • Start with 32 elements • 5 comparisons total until found • Note 25 = 32 log2 32 = 5 • Log2 of input size gives ~ number of comparisons 4 elements 2 elements 1 element
Big-Oh: Algorithm Complexity • Binary search is a O(logn) algorithm (logarithmic) O(1) O(logn) O(n) O(n2) O(2n) Binary search Sequential search Modifying a 2D image Traveling Salesman (brute force) constant-time logarithmic linear quadratic exponential
Some Comparisons • nis the size of the input binary search sequential search
Sequential vs. Binary Search • Sequential • O(n): linear • Easy to implement • Inefficient for large input size • Binary: O(logn) • O(logn): logarithmic • Reasonably efficient for large input size • Requires the list to be sorted