1 section 2.1 algorithms. 2 algorithm a finite set of precise instructions for performing a...

15
1 Section 2.1 Algorithms

Upload: james-fitzgerald

Post on 05-Jan-2016

215 views

Category:

Documents


2 download

TRANSCRIPT

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

1

Section 2.1

Algorithms

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

2

Algorithm

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

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

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

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

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

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

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

ax

x=1

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

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

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

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

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

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

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

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

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

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

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

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

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

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) / 2if (target > element at midpoint) then left = midpoint +

1

else right = midpoint

if (target = element at left index) then location = left index

else location = 0

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

13

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=6since 6th element (19) < 23, left = 7

2. Since 7<11 (left < right), midpoint=9since 9th element (56) > 23, right = 9

3. Since 7(left) < 9 (right), midpoint=8since 8th element (42) > 23, right = 8

Continued on next slide ...

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

14

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=7since 7th element (23) = 23, right = 7

5. Since 7(left)=7(right), drop out of loop

Since 23 = 23, location = left(7)

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

15

Section 2.1

Algorithms

-ends-