+ All Categories
Home > Documents > CS114-009 Class 22 Today A word from the Real World What happens when software goes bad… Binary...

CS114-009 Class 22 Today A word from the Real World What happens when software goes bad… Binary...

Date post: 17-Jan-2016
Category:
Upload: suzan-burns
View: 215 times
Download: 0 times
Share this document with a friend
Popular Tags:
20
CS114-009 Class 22 Today A word from the Real World What happens when software goes bad… Binary Search Announcements Exam 3 – Nov. 25 th in class Programming Project #6 due Nov. 20 by midnight e-mail to [email protected]
Transcript
Page 1: CS114-009 Class 22 Today  A word from the Real World What happens when software goes bad…  Binary Search Announcements  Exam 3 – Nov. 25 th in class.

CS114-009 Class 22 Today

A word from the Real World What happens when software goes bad…

Binary Search Announcements

Exam 3 – Nov. 25th in classProgramming Project #6 due Nov. 20 by midnight

e-mail to [email protected]

Page 2: CS114-009 Class 22 Today  A word from the Real World What happens when software goes bad…  Binary Search Announcements  Exam 3 – Nov. 25 th in class.

Programming project #6 Skeleton of program #6

Page 3: CS114-009 Class 22 Today  A word from the Real World What happens when software goes bad…  Binary Search Announcements  Exam 3 – Nov. 25 th in class.

When software goes wrong… What does software do?

Payrolls, bills, businesses… Phones & other ‘net’ systems Elevator controls Anti-lock brake controllers Insulin pumps, heart

pacemakers X-ray therapy equipment Aircraft control (“fly by wire”)

What happens when software fails? Bills and paychecks wrong When phones die… Elevators… Auto brakes… Insulin pumps… X-ray machines with bad

software have killed Therac-25

Aircraft Commercial passenger

planes have crashed

Page 4: CS114-009 Class 22 Today  A word from the Real World What happens when software goes bad…  Binary Search Announcements  Exam 3 – Nov. 25 th in class.

How important is it for software to work?

If software doesn’t work, Things go wrong, Businesses fail, People lose their jobs, People are injured, People even die…

Many people have died!

Page 5: CS114-009 Class 22 Today  A word from the Real World What happens when software goes bad…  Binary Search Announcements  Exam 3 – Nov. 25 th in class.

How can we make software work?

Careful planning: algorithms, good teaming skills, good communication

Teaching those skills Making sure students know what we’re

teaching.

Page 6: CS114-009 Class 22 Today  A word from the Real World What happens when software goes bad…  Binary Search Announcements  Exam 3 – Nov. 25 th in class.

A “better” algorithm: Merge Sort Basic idea:

A list of size one is sorted

It is easy to merge sorted lists into a larger sorted list

Algorithm Break list up into lots of

small (1 item) lists Merge these lists

Will discuss later

Example

90 5 35 25 20 50 10

5 10 20 25 35 50 90

Page 7: CS114-009 Class 22 Today  A word from the Real World What happens when software goes bad…  Binary Search Announcements  Exam 3 – Nov. 25 th in class.

Our basic search technique Assumes array is not

sorted On average, takes N/2

tries to find it (if it is there) Sometimes find it early Sometimes find it late On average, will find it

half-way through

Consider an array of 1,000,000 elements

Current technique averages 500,000 comparisons

Can do it in 20

early lateaverage

Page 8: CS114-009 Class 22 Today  A word from the Real World What happens when software goes bad…  Binary Search Announcements  Exam 3 – Nov. 25 th in class.

Efficient Searching Binary Search

Array must be sorted Compare search value

with the middle item Middle larger? Must be in

lower (first) half Middle smaller? Must be

in the upper (second) half

Examples Looking for 4, first half Looking for 57, last half

Basic Algorithm Find middle item Compare search value

Ignore one half of array

Repeat this process on the other half of the array until you find it (or realize it is not there)

1 4 7 10 14 28 37 44 46 57 61

Page 9: CS114-009 Class 22 Today  A word from the Real World What happens when software goes bad…  Binary Search Announcements  Exam 3 – Nov. 25 th in class.

Binary Search Example: find 55

1 2 3 5 8 13 21 34 55 56 57 60 65 72 81 90

1 2 3 5 8 13 21 34 55 56 57 60 65 72 81 90

1 2 3 5 8 13 21 34 55 56 57 60 65 72 81 90

Middle item is 34, less than 55, throw it and everything to the left of it out

1 2 3 5 8 13 21 34 55 56 57 60 65 72 81 90

Repeat: Middle item is 60, greater than 55, throw it and everything to the left of it out

Repeat: Middle item is 56, greater than 55, throw it and everything to the right of it out

Repeat: Middle item is 55, Found it !

Page 10: CS114-009 Class 22 Today  A word from the Real World What happens when software goes bad…  Binary Search Announcements  Exam 3 – Nov. 25 th in class.

Class Exercises

1 2 3 5 8 13 21 34 55 56 57 60 65 72 81 90

Find 81

Find 5

Find 50

Find 8 How many comparisons does it take to find each one (or

realize it is not there)?

Initial middle point

Page 11: CS114-009 Class 22 Today  A word from the Real World What happens when software goes bad…  Binary Search Announcements  Exam 3 – Nov. 25 th in class.

How fast is binary search? Quick math review

If log2N = K, then N = 2K

If K=5, N = 25 = 32 If K=10, N = 210 = 1024 If K=20, N = 220 =1048576

Compare sequential and binary search

Why is binary search O(log2 N) ? Throw away half of items

each time 1,000 500 250 …

4 2 1

Keep discarding half until down to single item

How many steps to get to one item (worst case?)

Takes log2 N steps to get down to one item

Complexity is O(log2 N)

N/2, avg sequential log2N, avg binary

100 / 2 7

10,000 / 2 13

100,000 / 2 17

1,000,000 / 2 20

Page 12: CS114-009 Class 22 Today  A word from the Real World What happens when software goes bad…  Binary Search Announcements  Exam 3 – Nov. 25 th in class.

Class Exercise Classic guessing game

Let a person pick a number in the range from 1 to 1000. You can guess it in ten or fewer attempts.

Try it Use the “binary search” principle:

Always guess the mid-point of the remaining numbers

One person picks a number from 1 to 1000 Have the others guess (tell them higher or lower) How many tries did it take?

Note: You can guess 1 – 1,000,000 in 20 attempts.

Page 13: CS114-009 Class 22 Today  A word from the Real World What happens when software goes bad…  Binary Search Announcements  Exam 3 – Nov. 25 th in class.

Review: Binary Search Finds in O(log2 N) time

N is the size of the data (length of the array)

What does this mean? Array has 1,000 elements

10 steps or less Array has 1,000,000 items

20 steps or less Eliminate half each time

Recursive solution Find the midpoint Compare our number

with the midpointif it’s the same, found it

If our item is less, recursively call binary search on left sidefrom start to mid-1

If our item is greater, recursively call binary search on right sidefrom mid+1 to the end

Page 14: CS114-009 Class 22 Today  A word from the Real World What happens when software goes bad…  Binary Search Announcements  Exam 3 – Nov. 25 th in class.

Our algorithm Function takes four

parameters Array Lowest point to search Highest point to search Item to find

Configuration int array[1000], item; array is in sorted order Looking for item

Initial callsearch(array, 0, 999, item)

Page 15: CS114-009 Class 22 Today  A word from the Real World What happens when software goes bad…  Binary Search Announcements  Exam 3 – Nov. 25 th in class.

Our algorithmint search (array [ ], firstIndex, lastIndex, value) {

if (firstIndex > lastIndex)item was not in the array

mid = (firstIndex+lastIndex) / 2if (value == array[mid])

found itelse if (value > array[mid])

search(array, mid+1, lastIndex, value)else // (value < array[mid])

search(array, firstIndex, mid-1, value)}

Page 16: CS114-009 Class 22 Today  A word from the Real World What happens when software goes bad…  Binary Search Announcements  Exam 3 – Nov. 25 th in class.

Tracing our algorithmint search (array [ ],

firstIndex,lastIndex,value) {

if (firstIndex > lastIndex)item was not in the array

mid = (firstIndex+lastIndex) / 2

if (value == array[mid])found it

else if (value > array[mid])

search(array, mid+1,lastIndex, value)

else // (value < array[mid])

search(array, firstIndex,mid-1, value)

array [4 7 42 55 73 88 93 104]value 73

search(array, 0, 7, 73)first <= last, not done yetmid = (0+7)/2 = 3array[mid] = 55value > 55, search upper half search(array, 4, 7, 73)

first <= last, not done yetmid = (4+7)/2 = 5array[mid] = 88value < 88, search lower

halfsearch(array, 4, 4, 73) first <= last, not done yet mid = (4+4)/2 = 4 array[mid] = 73 found it (value ==

array[mid])

Page 17: CS114-009 Class 22 Today  A word from the Real World What happens when software goes bad…  Binary Search Announcements  Exam 3 – Nov. 25 th in class.

Class Exercisesint search (array [ ],

firstIndex,lastIndex,value) {

if (firstIndex > lastIndex)item was not in the array

mid = (firstIndex+lastIndex) / 2cout << mid << endl;if (value == array[mid])

found itelse if (value > array[mid])

search(array, mid+1,lastIndex, value)

else // (value < array[mid])search(array, firstIndex,

mid-1, value)

array [4 7 42 55 73 88 93 104]

Using this same array, search for Trace the function, notice the

cout of mid. 7 93 50 104 42

Page 18: CS114-009 Class 22 Today  A word from the Real World What happens when software goes bad…  Binary Search Announcements  Exam 3 – Nov. 25 th in class.

The actual C++ implementationint search(

int array[ ],int first,int last,int key) {

if (first > last) return -1;

int mid = (first + last) / 2;

if (array[mid] == key)return mid;

if (array[mid] > key)return search(array,

first, mid-1, key);else

return search(array,mid+1, last, key);

}

1. Takes 4 Parameters

2. Check for “not found”

3. Find midpoint

4. Check midpoint

5. Less than midpoint, look in lower half

6. Greater than midpoint, look in upper half

Page 19: CS114-009 Class 22 Today  A word from the Real World What happens when software goes bad…  Binary Search Announcements  Exam 3 – Nov. 25 th in class.

Class Exercises The file Binary-Search.cpp (on our class web

page) has an implementation of binary search.

The programUses an array of 10 integersPrompts you for a search valueShows the series of calls that are made during

the search for this number Download, compile, and run this program

Make sure you understand what it is doing

Page 20: CS114-009 Class 22 Today  A word from the Real World What happens when software goes bad…  Binary Search Announcements  Exam 3 – Nov. 25 th in class.

End of Class 22


Recommended