Binary Search

advertisement
Binary Search
Once a list is sorted using selectionsort or some other algorithm, a program might need to locate an
item in the list. For example, a program containing a list of employees might need to locate a specific
person in the list. It could simply look through the list until it finds the item it wants, but there is a
much faster method.
Binary search compares the target value to the item in the middle of the list. If the target is smaller
than the middle item, the search examines the items in the left half of the list. If the target is bigger
than the middle item, the search continues in the right half of the list. If the target matches the middle
item, the search is finished.
Each time the algorithm compares the target to the middle item, it divides the list in two halves. It
figures out which half contains the target, and focuses on just that half. This process is called binary
subdivision and it is a powerful search technique. Figure 3 shows a binary search graphically.
Figure 3: Binary search for the value 64.
The algorithm uses variables min and max to keep track of the indexes of items that might contain the
target. As it progresses, the algorithm updates min and max so it searches the appropriate halves of
the list. While the idea is straightforward, the details can be a little tricky. If the algorithm does not
properly update min and max, it may exclude the index that contains the target so it will be unable to
find the correct entry.
Eventually the algorithm either finds the item or it raises min and lowers max until min > max. At that
point the algorithm concludes that the target is not in the list. Figure 4 shows
the VBA code for binary search.
1.
Dim list (20) as integer
2. Max =ubound(List)
3. Min =lbound(list)
‘__________________________________________________
4. For a = min to max
5. List(a)=inputbox(“Enter value into the array)
6. Next a
‘__________________________________________________
7. ‘bubble sort
8. For ABC = min To max-1
9. ' counted loop from 1 past ABC to the end
10.
For cnt = (ABC + 1) To max
11.
'compare which number is greater to put highest on top
12.
If List (ABC) < List (cnt) Then
13.
'swap the numbers
14.
temp = List (ABC)
15.
List (ABC) = List (cnt)
16.
List (cnt) = temp
17.
End If
18.
Next cnt
19. Next ABC
‘__________________________________________________
20. Target = inputbox(“what value are you looking for?”)
21. Found = -99
‘ _____________________________________________________________
22. ‘binary Search
23. ' During the search, min <= target index <= max.
24. Do While min <= max
25.
middle = (min + max) / 2
26.
If target = list(middle) Then
27.
' We found it.
28.
found = middle
29.
Exit do
30.
ElseIf target < list(middle) Then
31.
' Search the left half.
32.
max = middle - 1
33.
Else
34.
' Search the right half.
35.
min = middle + 1
36.
End If
37. Loop
‘__________________________________________________
38. If found=-99 then
39.
Print”Not Found”
40. Else
41. Print target &”is in “ &found
42. End if
Figure 4: Binary search.
Each time it examines an item, binary search divides the list of entries that might contain the target in half. If the list contains
N items, this process can continue for only log(N) steps before there is only one item remaining to consider. That means the
algorithm runs in O(log(N)) time. This is extremely fast. If the list contains 1 million items, the algorithm must examine at
most log(1,000,000) or about 20 items.
Download