Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Section 2.1 Algorithms. 2 Algorithm A finite set of precise instructions for performing a computation or for solving a problem.

Similar presentations


Presentation on theme: "1 Section 2.1 Algorithms. 2 Algorithm A finite set of precise instructions for performing a computation or for solving a problem."— Presentation transcript:

1 1 Section 2.1 Algorithms

2 2 Algorithm A finite set of precise instructions for performing a computation or for solving a problem

3 3 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

4 4 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

5 5 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  a x x=1

6 6 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

7 7 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

8 8 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

9 9 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

10 10 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

11 11 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

12 12 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

13 13 Binary Search Example Given the following ordered set: a 1 a 2 a 3 a 4 a 5 a 6 a 7 a 8 a 9 a 10 a 11 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...

14 14 Binary Search Example Given the following ordered set: a 1 a 2 a 3 a 4 a 5 a 6 a 7 a 8 a 9 a 10 a 11 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)

15 15 Section 2.1 Algorithms -ends-


Download ppt "1 Section 2.1 Algorithms. 2 Algorithm A finite set of precise instructions for performing a computation or for solving a problem."

Similar presentations


Ads by Google