Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

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  Programming Project #6 due Nov. 20 by midnight e-mail to vrbsky@cs.ua.eduvrbsky@cs.ua.edu

2 Programming project #6project #6 Skeleton of program #6program #6

3 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

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

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

6 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

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

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

9 Binary Search Example: find 55 123581321345556576065728190 123581321345556576065728190 123581321345556576065728190 Middle item is 34, less than 55, throw it and everything to the left of it out 123581321345556576065728190 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 !

10 Class Exercises 123581321345556576065728190 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

11 How fast is binary search? Quick math review  If log 2 N = K, then N = 2 K If K=5, N = 2 5 = 32 If K=10, N = 2 10 = 1024 If K=20, N = 2 20 =1048576 Compare sequential and binary search Why is binary search O(log 2 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 log 2 N steps to get down to one item Complexity is O(log 2 N) N/2, avg sequentiallog 2 N, avg binary 100 / 27 10,000 / 213 100,000 / 217 1,000,000 / 220

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

13 Review: Binary Search Finds in O(log 2 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 midpoint if it’s the same, found it  If our item is less, recursively call binary search on left side from start to mid-1  If our item is greater, recursively call binary search on right side from mid+1 to the end

14 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 call search(array, 0, 999, item)

15 Our algorithm int 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) }

16 Tracing our algorithm int 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 55, search upper half search(array, 4, 7, 73) first <= last, not done yet mid = (4+7)/2 = 5 array[mid] = 88 value < 88, search lower half search(array, 4, 4, 73) first <= last, not done yet mid = (4+4)/2 = 4 array[mid] = 73 found it (value == array[mid])

17 Class Exercises int search (array [ ], firstIndex, lastIndex, value) { if (firstIndex > lastIndex) item was not in the array mid = (firstIndex+lastIndex) / 2 cout << mid << endl; 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] Using this same array, search for Trace the function, notice the cout of mid.  7  93  50  104  42

18 The actual C++ implementation int 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

19 Class Exercises The file Binary-Search.cpp (on our class web page) has an implementation of binary search.Binary-Search.cpp The program  Uses an array of 10 integers  Prompts you for a search value  Shows 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

20 End of Class 22


Download ppt "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."

Similar presentations


Ads by Google