150 likes | 219 Views
Section 2.1. Algorithms. Algorithm. A finite set of precise instructions for performing a computation or for solving a problem. Properties of an Algorithm. Input: values from a specified (finite) set Output: solution to problem for specified set
E N D
Section 2.1 Algorithms
Algorithm • A finite set of precise instructions for performing a computation or for solving a problem
Properties of an Algorithm • Input: values from a specified (finite) set • Output: solution to problem for specified set • Definiteness: steps precisely defined, unambiguous • Correctness: produces correct output for specified input
Properties of an Algorithm • Finiteness: finite number of steps required to produce output • Effectiveness: must be possible to perform each step exactly in finite amount of time • Generality: should be applicable to all problems of its form, not just specific inputs
Example: Algorithm for finding sum of finite integer sequence 1. Assign 0 to sum 2. While there are integers to examine, add next integer to sum 3. When all integers have been examined, sum holds sum of all values n ax x=1
Properties applied to example • Input: sequence of integers • Output: sum of sequence values • Definiteness: each step spelled out unambiguously • Finiteness: terminates when all integers in sequence have been read • Effectiveness: each step is finite • Generality: works for any finite integer sequence
Searching Algorithms • Searching problem: finding an element in an ordered list • Output is position of element found (0 if element is not present) • Linear and Binary Search are examples
Linear (Sequential) Search • Algorithm: • Examine each item in succession to see if it matches target value • If target found or no elements left to examine, stop search • If target was found, location = found index; otherwise, location = 0 • Works whether or not list is ordered; just as efficient (or inefficient) either way
Pseudocode for Linear Search LinearSearch (inputs: target, set of N values to search) foundIndex =1 (start at beginning of list) while (N foundIndex and target current value) increment foundIndex if (N foundIndex) then location = foundIndex else location = 0
Binary Search • Requires elements in list to be sorted • Algorithm we’ll look at is different from the one we use in CS2 - this one is not recursive • Classic example of divide-and-conquer algorithm
Binary Search • Works by splitting list in half, then examining the half that might contain the target value • if not found, split and examine again • eventually, set is split down to one element • If the one element is the target, set location to index of item; otherwise, location = 0
Pseudocode for Binary Search BinarySearch (inputs: target, sorted list of N elements) left = 1 (left endpoint of search interval) right = N (right endpoint) while (left < right) midpoint = (left + right) / 2 if (target > element at midpoint) then left = midpoint + 1 else right = midpoint if (target = element at left index) then location = left index else location = 0
Binary Search Example Given the following ordered set: a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 2 4 11 12 17 19 23 42 56 60 91 Find target value: 23 1. N=11, left=1, right=11; since 1<11, midpoint=6 since 6th element (19) < 23, left = 7 2. Since 7<11 (left < right), midpoint=9 since 9th element (56) > 23, right = 9 3. Since 7(left) < 9 (right), midpoint=8 since 8th element (42) > 23, right = 8 Continued on next slide ...
Binary Search Example Given the following ordered set: a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 2 4 11 12 17 19 23 42 56 60 91 Find target value: 23 4. Since 7(left) < 8(right), midpoint=7 since 7th element (23) = 23, right = 7 5. Since 7(left)=7(right), drop out of loop Since 23 = 23, location = left(7)
Section 2.1 Algorithms -ends-