Programming and Problem Solving with Java

(やまだぃちぅ) #1
11.6 S e a rching | 553

(A-M)

(A-G)

(A-C) (D-G)

Figure 11.8 A Binary Search of the Phone Book


Binary Search
Set first to 0
Set last to numItems – 1
Set found to false
whilesearch area is not empty and !found
Set middle to (first + last) divided by 2
if(item is equal to listItems[middle])
Set found to true
else if(item is less than listItems[middle])
Set last to middle – 1 // look in first half
else
Set first to middle + 1 // look in last half
Free download pdf