CP114 Lecture Notes - Lecture 4: Binary Search Algorithm, Data Element, Linear Search

14 views3 pages
School
Course
Professor

Document Summary

This means it must find the first occurrence of a series of equal values. The position of those values must be preserved by the list sort. One version of binary search performs two comparisons (greater than and less than) with respect to the midpoint. If both those comparisons fail, the key value and the midpoint value must be equal. However, there is no guarantee that the value found this way is the first in the list with that value. i = -1 low = 0. # index of end of subarray to search. high = len( self. _values ) - 1 while low < high and i = -1: # (avoids overflow on large values vs (low + high) // 2 mid = ( high - low ) // 2 + low if self. _values[mid] > key: # search the left subarray. high = mid - 1 elif self. _values[mid] < key: # value has been found. i = mid return i.

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents