+ All Categories
Home > Documents > QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP...

QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP...

Date post: 29-May-2020
Category:
Upload: others
View: 4 times
Download: 0 times
Share this document with a friend
109
QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ‘A’ if the character was A, or ‘not A’ otherwise. 1 What is the hex ASCII code for A? Use this code as template:
Transcript
Page 1: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

QUIZ on Ch.6:

Write a PEP assembly program that reads in a character, and then outputs ‘A’ if the character was A, or ‘not A’ otherwise.

1

What is the hex ASCII code for A?

Use this code as template:

Page 2: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,
Page 3: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

Problem Solving

How to Solve It: A New Aspect of Mathematical Method by George Polya, 1945

The book is written within the context of solving mathematical problems, but the methods described are applicable to problem solving in general.

3

We can use the methods described

by Polya to solve any computer-

related problem!

Page 4: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

1. Understand the problem

– What are the hypotheses? Data? Unknowns?

2. Devise a plan

– Can we find a related problem? A sub-problem?

– Can we strengthen or relax the hypotheses to obtain an easier problem?

3. Carry out the plan

— Prove that each step is correct!

4. Look back

– Check result

– Find shortcuts and alternate solutions

– Generalize to related problems4

Page 5: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

Strategies for step 2 (devise a plan)

Ask questions!

– What do I know about the problem?

– What is the information that I have to process in order the find the solution?

– What does the solution look like?

– What sort of special cases exist?

– How will I recognize that I have found the solution?

5

Page 6: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

Strategies

Never reinvent the wheel!

Similar problems come up again and again in different guises

A good programmer recognizes a task or subtask that has been solved before and plugs in the solution

Can you think of two similar problems we solved in Python?

6

Page 7: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

StrategiesDivide and Conquer!

Break up a large problem into smaller sub-problems and solve each separately

– Applies the concept of abstraction

– The divide-and-conquer approach can be applied over and over again until each subtask is manageable

7

See funny image!

Page 8: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

Polya’s 4 steps can be applied to Computer Problem Solving

8

Analysis and Specification Phase

Ask questions to understand all the requirements

Explain in detail what needs to be achieved

Algorithm Development Phase

Develop algorithm

Test algorithm

Implementation Phase

Code algorithm

Test the code in various ways

Maintenance Phase

Use the code, find bugs

Fix bugs

Code new features, as requested by users

Page 9: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

QUIZ: Match the steps in Polya’s method to the ones in the computer method for

problem solving

9

Analysis and Specification

Implementation

Algorithm Development

Maintenance

Devise a plan

Look back

Understand

Carry out the plan

Page 10: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

Phase Interactions

10

Page 11: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

Algorithms

Algorithm = A set of unambiguous instructions for solving a problem in a finite amount of time, using a finite amount of data

11

Page 12: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

Algorithm = A set of unambiguous instructions for solving a problem in a finite amount of time, using a finite amount of data

12Image source: http://my-online-log.com/tech/archives/1214

Page 13: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

Algorithm = A set of unambiguous instructions for solving a problem in a finite amount of time, using a finite amount of data

Abstract Step

An algorithmic step containing unspecified details

Concrete Step

An algorithm step in which all details are specified

13

Whether the step is abstract or concrete depends on the

programming language we’re using!

Page 14: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

Algorithm = A set of unambiguous instructions for

solving a problem in a finite amount of time,using a finite amount of data

Douglas Hofstadter

“Godel, Escher, Bach”

The story of Alladin and G.O.D.

(G.O.D. Over Djinn –

recursive acronym!)

Page 15: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

Algorithm = A set of unambiguous instructions for solving a problem in a finite amount of time, using a

finite amount of data

Douglas Hofstadter

“Godel, Escher, Bach”

The story of Alladin and G.O.D.

Page 16: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

7.2 Algorithms with simple variables

Variable = a means of storing intermediate results from one task to the next.

At the hardware level, a simple variable is just one or several adjacent Bytes in the computer memory.

Q: How many Bytes does a simple variable have in PEP/8?

16

Page 17: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

Algorithms with Selection Statements (a.k.a. decision or if)

17

Flowchart of if statement

Figure not in text

Page 18: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

18

Algorithm with Selection

Problem: Write the appropriate dress for a given

temperature.

Write "Enter temperature"

Read temperature

Determine Dress

Which statements are concrete?

Which statements are abstract?

Algorithm Determine Dress v.1

Computer language is Python from now on!

Page 19: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

19

Write “Enter temperature”

Read temperature

IF (temperature > 90)

Write “Texas weather: wear shorts”

ELSE IF (temperature > 70)

Write “Ideal weather: short sleeves are fine”

ELSE IF (temperature > 50)

Write “A little chilly: wear a light jacket”

ELSE IF (temperature > 32)

Write “Philadelphia weather: wear a heavy coat”

ELSE

Write “Stay inside”

Algorithm Determine Dress v.2

Is this concrete enough for implementation in Python?

Page 20: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

Algorithms with Loops (a.k.a. repetition)

20

Flow of control of while statement

Figure not in text

Page 21: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

QUIZ: There are loops that can execute 0 times and loops that must execute at least 1 time.Which type is this?

21

Page 22: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

Answer: It depends on whether the decision (diamond) is at the beginning or at the end of the loop!

22

Not in text

Page 23: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

Loops in Python

23

Not in text

Page 24: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

Extra-credit question:

24

Page 25: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

Event-controlled Loops, a.k.a. WHILE loops

25

They’re the most general type of loops!

Set sum to 0

Set allPositive to true

WHILE (allPositive)

Read number

IF (number > 0)

Set sum to sum + number

ELSE

Set allPositive to false

Write "Sum is " + sum

Page 26: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

Counter-controlled Loops

26

They are a particular case of event-controlled

loops: the event is that a counter variable has

reached a predetermined limit

Set sum to 0

Set limit to 42

Set count to 1

While (count <= limit)

Read number

Set sum to sum + number

Increment count

Write "Sum is " + sum

Page 27: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

Counter-controlled Loops

27

They are a particular case of event-controlled

loops: the event is that a counter variable has

reached a predetermined limit

Set sum to 0

Set limit to 42

Set count to 1

While (count <= limit)

Read number

Set sum to sum + number

Increment count

Write "Sum is " + sum

For loops are counter-

controlled!

Page 28: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

28

Important application of looping: Successive approximation

algorithms

Read in square

Calculate the square root

Write out square and the square root

Algorithm Calculate Square Root v.1

Which steps are abstract and which concrete?

Page 29: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

29

Set epsilon to 1

WHILE (epsilon > 0.001)

Calculate new guess

Set epsilon to abs(square - guess * guess)

Which steps are abstract and which concrete?

Algorithm Calculate Square Root v.2

In Python usemath.fabs(…)

A more appropriate name for guess would be approximation

Page 30: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

30

Set newGuess to

(guess + (square/guess)) / 2.0

Set guess to square/4

Set epsilon to 1

What’s the mathematical formula for the new approximation?

How do we get the loop started?

Algorithm Calculate Square Root - Refinements in v.2

Page 31: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

31

Read in square

Set guess to square/4

Set epsilon to 1

WHILE (epsilon > 0.001)

Set guess to (guess + (square/guess)) / 2.0

Set epsilon to abs(square - guess * guess)

Write out square and guess

Which steps are abstract and which concrete?

Algorithm Calculate Square Root v.3

Page 32: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

32

Set newGuess to

(guess + (square/guess)) / 2.0

QUIZ: Square root algorithms

(approximations)

We want to calculate = 2.236…

Set your initial guess x0 = 1 and show the

next 3 approximations x1, x2, x3

5

Page 33: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

33

Set newGuess to

(guess + (square/guess)) / 2.0

QUIZ: Square root algorithm

(approximations)

We want to calculate = 2.236…

Set your initial guess x0 = 42 and show the

next 3 approximations x1, x2, x3

5

EOL1

Page 34: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

QUIZ

• 16: Count-controlled loops repeat a specific number of times.

• 17: Event-controlled loops repeat a specific number of times

• 18: Count-controlled loops are controlled by a counter

• Count-controlled loops are more general than event-controlled loops

34

Page 35: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

35

Set newGuess to

(guess + (square/guess)) / 2.0

QUIZ: Square root algorithms

(approximations)

We want to calculate = 3.162…

Set your initial guess x0 = 10/4 and show

the next 3 approximations x1, x2, x3

10

Page 36: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

Set newGuess to

(guess + (square/guess)) / 2.0

QUIZ: Square root algorithms

(approximations)

We want to calculate = 3.16227…

Set your initial guess x0 = 10/4 and show

the next 3 approximations x1, x2, x3

10

x0 = 2.5 x1 = 3.25 x2 = 3.16346… x3 = 3.16227…

Page 37: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

37

QUIZ: Square root algorithm

We want to calculate = 2.6457…

Set your initial guess to x0 = 7/4 = 1.75

and show the next 3 approximations x1, x2, x3

7

Page 38: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

38

QUIZ: Square root algorithm

We want to calculate = 2.645751…7

Page 39: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

39

Remember: The

algorithm converges

irrespective of the

initial guess x0!

= 2.645751…7

Page 40: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

40

QUIZ:

Re-write this program

using a for loop!

Page 41: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

QUIZ:

Re-write this program

using a for loop!

Page 42: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

Individual work for

next time:

Re-write this program

using a while loop!

Page 43: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

7.3 Composite Data Types

They can be classified according to many criteria:

• homogeneous vs. heterogeneous

• accessed by index vs. accessed by name

• mutable vs. immutable

• linear vs. non-linear

• etc.

43

Page 44: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

Composite Data Types

Records

A named heterogeneous collection of items in which individual items are accessed by name. For example, we could bundle name, age and hourly wage items into a record named Employee

Arrays

A named homogeneous collection of items in which an individual item is accessed by its position (index) within the collection

44

Are these the lists from Python? Why not?

Python strings are arrays of charactersCan implement in Python w/lists …

Page 45: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

Composite Data Types

Lists (will be covered in next chapter)

A named heterogeneous collection of items in which individual items are accessed by position (index).

We have them in Python, e.g.

>>> myList = [“dog”, 42, 51.375, [1,2]]

>>> myList[1]

42

45

Page 46: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

Composite Data Types - Records

Employee

name

age

hourly/Wage

Algorithm to store values into the fields of record:

Employee employee // Declare an Employee variableSet employee.name to “Frank Jones”Set employee.age to 32Set employee.hourlyWage to 27.50

46

Page 47: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

Composite Data Types - Arrays

47

numbers[0]

numbers[4]

numbers

Page 48: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

Some items in an array may be unused at a given time

48

first

last

numbers

??

??

??

??

Page 49: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

Useful Algorithms on Arrays

• Initializing all items

• Printing all items

• Searching for an item

• Sorting the array

49

Page 50: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

50

Initializing arrays

integer data[20]

Write “How many values?”

Read length

Set index to 0

WHILE (index < length)

Read data[index]

Set index to index + 1

Fill an array numbers with length values

that are being input from the keyboard

Page 51: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

51

QUIZ

integer data[20]

Write “How many values?”

Read length

Set index to 0

WHILE (index < length)

Read data[index]

Set index to index + 1

Modify this pseudocode to print the values

after initializing them.

Page 52: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

An Unsorted Array

52

data

Page 53: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

A Sorted Array

53

data

Page 54: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

Sorted Arrays

• The values stored in an array have unique keysof a type for which the relational operators are defined.

• Sorting rearranges the elements into either ascending or descending order within the array.

54

Reality check: In a real-life problem it’s very common

to encounter repeated keys!

Page 55: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

7.4 Search algorithms

55

Page 56: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

Sequential Search inUnsorted Array

A sequential search examines each item in turn and compares it to the one we are searching.

If it matches, we have found the item. If not, we look at the next item in the array.

We stop either when we have found the item or when we have looked at all the items and not found a match.

Thus, we have a loop with two ending conditions.

56

Page 57: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

Set position to 0

Set found to FALSE

WHILE (position < length AND NOT found )

IF (numbers[position] equals searchItem)

Set found to TRUE

ELSE

Set position to position + 1

57

The array’s name is numbersThe value we’re searching for is stored in searchItem

Page 58: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

Set position to 0

Set found to FALSE

WHILE (position < length AND NOT found )

IF (numbers[position] equals searchItem)

Set found to TRUE

ELSE

Set position to position + 1

58

QUIZ: When the loop exits, what do we need to do?

Page 59: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

Set index to 0

Set found to FALSE

WHILE (index < length AND NOT found )

IF (data[index] equals searchItem)

Set found to TRUE

ELSE

Set index to index + 1

59

QUIZ: Desk-check this algorithm for the array

The item searched is:

• 35

• 43

42

100

35

1

EOL 2

Page 60: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

QUIZ

-- Define arrays

-- What are the 4 fundamental types of algorithms

used to manipulate arrays?

-- What control structure is normally used to

access the elements of an array?

-- Explain sequential search in your own words

(unsorted array)

60

Page 61: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

Sequential Search in Sorted Array

Idea:

If items in an array are sorted, we can stop looking when we pass the place where the item would be if it were present in the array

61

Is this better?

Page 62: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

62

Sequential Search in Sorted Array

Set index to 0

Set found to FALSE

WHILE (index < length AND NOT found)

IF (data[index] equals searchItem)

Set found to TRUE

ELSE IF (data[index] > searchItem)

Set index to length

ELSE

Set index to index + 1

This is the new part!

(Compare with previous alg. for unsorted array)

This alg. is called sequential search with early termination

Page 63: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

QUIZ:End-of-chapter question 66 a

63

SearchItem = 4

SearchItem = 49

SearchItem = 50

Page 64: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

Binary Search in Sorted Array

Search begins at the middle and finds the item or eliminates half of the unexamined items; the process is then repeated on the half where the item might be

64

24 30 31 42 44 90 92 94 99

Example: searchItem = 42

Page 65: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

Binary Search Algorithm

65

Set first to 0

Set last to length-1

Set found to FALSE

WHILE (first <= last AND NOT found)

Set middle to (first + last)/ 2

IF (item equals data[middle]))

Set found to TRUE

ELSE

IF (item < data[middle])

Set last to middle – 1

ELSE

Set first to middle + 1

RETURN found

IntegerDivision!

Page 66: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

Binary Search example

66Figure 7.10 Trace of the binary search

rat

Page 67: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

QUIZ Binary Search

67

Search for deer

Page 68: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

8.5 Sorting algorithms

68

Page 69: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

Sorting

Sorting

Arranging items in a collection so that there is an ordering on one (or more) of the fields in the items

Sort Key

The field (or fields) on which the ordering is based

Sorting algorithms

Algorithms that order the items in the collection based on the sort key

69

Page 70: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

Selection Sort

70

Figure 7.11 Example of a selection sort (sorted elements are shaded)

Page 71: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

71

100

10

11

12

35

200

13

2

Show the swapped elements with arrows.

Show the sorted elements with shading.

QUIZ Selection Sort

Page 72: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

solution

72

Page 73: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

73

24

10

11

12

35

20

1

2

Show the swapped elements with arrows.

Show the sorted elements with shading.

Individual work for next time

Page 74: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

74

solution

Page 75: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

75

Selection Sort Pseudocode

Selection Sort

Set firstUnsorted to 0

WHILE (not sorted yet)

Find smallest unsorted item

Swap firstUnsorted item with the smallest

Set firstUnsorted to firstUnsorted + 1

Not sorted yet

current < length – 1

Red steps

are

abstract

(not

concrete)

Page 76: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

76

Find smallest unsorted item

Set indexOfSmallest to firstUnsorted

Set index to firstUnsorted + 1

WHILE (index <= length – 1)

IF (data[index] < data[indexOfSmallest])

Set indexOfSmallest to index

Set index to index + 1

Set index to indexOfSmallest

Swap firstUnsorted with smallest

Set tempItem to data[firstUnsorted]

Set data[firstUnsorted] to data[indexOfSmallest]

Set data[indexOfSmallest] to tempItem

Page 77: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

SKIP Bubble Sort and Insertion Sort, just remember that they are as fast

(efficient) as Selection Sort

77

Is it possible to devise a more efficient sorting algorithm?

Page 78: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

78

Is it possible to devise a more efficient sorting algorithm?

Yes, but, in order to do this, we have to understand subprograms and

recursion.

Page 79: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

7.6 Recursive Algorithms

To understand Quick Sort, we need:

• a new control structure: subprogram

• a new concept: recursion

79

Page 80: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

Subprogram Statements

We can give a section of code a name and use that name as a statement in another part of the program

When the name is encountered, the processing in the other part of the program halts while the named code is executed

When execution is finished, the first part of the program resumes execution

That section of code is called a subprogram80

Page 81: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

Subprogram Statements

81Figure 7.14 Subprogram flow of control

Page 82: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

We already used subprograms in Python!

… we called them

• Functions

– int() float() ord() chr() str() etc.

• Methods

– list.append() string.upper() string.find() etc.

82

Do you remember what each of them

does?

Page 83: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

Subprogram Statements

What if the subprogram needs data from the calling unit? This data is called input.

Parameters

Identifiers listed in parentheses beside the subprogram declaration; sometimes called formal parameters

Arguments

Identifiers listed in parentheses on the subprogram call; sometimes called actual parameters

83

Page 84: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

Parameters and arguments in Python

def disp(a, b):

print a, 'and', b

x, y = 1, 2

disp(x, y)

84

Page 85: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

What if the subprogram needs to give data back to the calling unit? This data is called output.

Void subprograms

They do not return a value, just perform certain actions

>>> print(“Enter a positive integer”)

>>> printer(3, 4)

Value-returning subprograms

>>> a = input(“Enter a positive integer”)

The keyword RETURN is used in many programming languages

85

Page 86: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

What if the subprogram needs to give data back to the calling unit? This data is called output.

Void subprograms

They do not return a value, just perform certain actions

>>> print(“Enter a positive integer”)

>>> printer(3, 4)

Value-returning subprograms

>>> a = input(“Enter a positive integer”)

The keyword RETURN is used in many programming languages

86

Do not confuse returning values with printing or displaying values. Returning means that a value is

given back to the main program!

Page 87: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

Value-returning function in Python

def sum(a, b):

return a + b

x, y = 1, 2

print sum(x, y)

87

Compare to the previous function, which is void

(does not return anything)

Page 88: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

Value-returning and void Subprograms

88

Page 89: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

Subprogram Statements

Subprograms are very important tools for abstraction.

Other popular names for subprograms:

• sub

• subroutine

• function

• procedure

• module

• method

89

Page 90: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

Recursion

Recursion

The ability of a subprogram to call itself

Base case

The case to which we have an answer

General case

The case that expresses the solution in terms of a call to itself with a smaller version of the problem

90

Page 91: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

Recursion The factorial of a positive integer is defined as the integer times the product of all the integers between itself and 0:

N! = 123 … N

But an alternate recursive definition is possible:

N! = N(N 1)!

Base case

Fact(0) = 1 (0! is 1)

General Case

Fact(N) = N Fact(N-1) (for N ≥ 1)

91EOL 4

Page 92: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

QUIZ Binary Search

How many comparisons are needed to find the key 9 (or

decide that it is not in the array)?

Show the left, right and middle at each step.

Page 93: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

QUIZ Binary Search

How many comparisons are needed to find the key 9 (or

decide that it is not in the array)?

Show the left, right and middle at each step.

Exactly! The array must be sorted first! …

Page 94: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

QUIZ Binary Search

-50 -28 -23 -10 -4 0 3 41 28 37 60 155 200

How many comparisons are needed to find the key 60 in

this array?

Show first, last, and middle for each iteration.

Page 95: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

QUIZ Binary Search

-50 -28 -23 -10 -4 0 3 41 28 37 60 155 200

How many comparisons are needed to find the key 60 in

this array?

Show first, last, and middle for each iteration.

Array must be sorted first for Binary Search!

Page 96: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

Two definitions for the factorial Iterative: N! = 123 … N

Recursive: N! = N(N 1)!

Base case

Fact(0) = 1 (0! is 1)

General Case

Fact(N) = N Fact(N-1) (for N ≥ 1)

96

Page 97: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

Is this a valid recursion?

Source: http://www.urbandictionary.com/define.php?term=recursion97

Page 98: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

Base case

Fact(0) = 1

General Case

Fact(N) = N Fact(N-1)

98

How does the

genie calculate

Fact(3)?

Page 99: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

Your turn!N! = N * (N 1)!

Base case

Facto(0) = 1 (0! is 1)

General Case

Fact(N) = N * Fact(N-1) (for N ≥ 1)

Calculate:

0! =

1! =

2! =

5! = 99

Page 100: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

100

Recursive Factorial algorithm

Write “Enter n”Read nSet result to Factorial(n)Write result + “ is the factorial of “ + n

Factorial(n)IF (n equals 0)

RETURN 1ELSE

RETURN n * Factorial(n-1)

Page 101: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

101

QUIZ: Write this recursive function in Python

Write “Enter n”Read nSet result to Factorial(n)Write result + “ is the factorial of “ + n

Factorial(n)IF (n equals 0)

RETURN 1ELSE

RETURN n * Factorial(n-1)

Page 102: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

solution

102

Page 103: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

103

Recursive Binary Search BinarySearch (first, last)

IF (first > last)

RETURN FALSE

ELSE

Set middle to (first + last)/ 2

IF (item equals data[middle])

RETURN TRUE

ELSE

IF (item < data[middle])

BinarySearch (first, middle – 1)

ELSE

BinarySearch (middle + 1, last)

Page 104: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

SKIP QUICKSORT …Just remember that it is faster

(more efficient) than Selection Sort

104

Page 105: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

SKIP 7.7 Important Threads

105

Page 106: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

Read and take notes:Ethical Issues

Open-Source Software Development

What are the advantages and disadvantages of open-source software?

What does the success of Linux suggest about the future of open-source software?

Should open-source software be licensed and subject to standard copyright laws?

106

Page 107: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

Chapter review questions• Describe the computer problem-solving process and relate it

to Polya’s How to Solve It list

• Distinguish between a simple type and a composite type

• Distinguish between a void subprogram and a value-returning subprogram

• Recognize a recursive problem and write a recursive algorithm to solve it

• Distinguish between an unsorted array and a sorted array

• Describe the Quicksort algorithm

• Apply the linear search, binary search, selection sort and Quicksort to an array of items by hand

• What are the advantages and disadvantages of open-source software?

107

Page 108: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

Who am I?

108

I am a

mathematician.

Why is my

picture in a

book about

computer

science?

Page 109: QUIZ on Ch.6: Write a PEP assembly program that reads in a ... · QUIZ on Ch.6: Write a PEP assembly program that reads in a character, and then outputs ZA if the character was A,

Homework:

End-of-chapter questions 30, 31, 34, 35, 36, 66 b, c

Not from text: Calculate 7! by hand, showing all the recursive steps, as done in the lecture examples.

109Lecture 5 continues with ch.8


Recommended