Exam 3- 2011

Consider the following sorted list breakfast bacon

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: ext iteration. CS 115 Exam 3, Fall 2011 Page 4 of 11 INITIAL ORDER AFTER i=0 ITERATION AFTER i=1 AFTER i=2 AFTER i=3 coffee pancakes waffles juice banana yogurt cereal bacon toast donuts CS 115 Exam 3, Fall 2011 Page 5 of 11 Problem 3: 15 points. Consider the following sorted list: breakfast = ['bacon', 'banana', 'cereal', 'coffee', 'donuts', 'juice', 'pancakes', 'toast', 'waffles', 'yogurt'] and the following binary search (which is essentially identical to your lab code): # binary_search() # Finds the position of an item in a list # Parameters: the list; the item to search for # Returns: the item’s position (or None) def binary_search(search_list, value_to_find): first = 0 last = len(search_list) – 1 while first <= last: middle = (first + last) // 2 if value_to_find == search_list[middle]: return middle elif value_to_find < search_list[middle]: last = middle – 1 else: first = middle + 1 return None You may want to label...
View Full Document

This note was uploaded on 02/08/2014 for the course CS 115 taught by Professor Staff during the Fall '08 term at Sonoma.

Ask a homework question - tutors are online