Problem

Binary Search An especially efficient technique for searching an ordered list of ite...

Binary Search An especially efficient technique for searching an ordered list of items is called a binary search. A binary search looks for a value by first determining in which half of the list it resides. The other half of the list is then ignored, and the retained half is temporarily regarded as the entire list. The process is repeated until the item is found or the entire list has been considered. Use the algorithm and flowchart for a binary search shown below to rewrite the btnSearch_Click event procedure from Example 4 of Section 6.3.

Figure 6.72 shows a partial flowchart for a binary search. (The sought-after value is denoted by quarry. The Boolean variable flag keeps track of whether or not quarry has been found.) The algorithm for a binary search of the items in an ordered list box is as follows:

(i) At each stage, denote the index of the first item in the retained list by first and the index of the last item by last. Initially, set the value of first to 0, set the value of last to one less than the number of items in the list, and set the value of flag to False. (ii) Look at the middle item of the current list—the item having index middle = CInt((first + last)/2).

(iii) If the middle item is quarry, then set flag to True and end the search.

(iv) If the middle item is greater than quarry, then quarry should be in the first half of the list. So the index of quarry must lie between first and middle – 1. Set last to middle – 1.

(v) If the middle item is less than quarry, then quarry should be in the second half of the list of possible items. So the index of quarry must lie between middle + 1 and last. Set first to middle + 1.

(vi) Repeat steps (ii) through (v) until quarry is found or until the halving process uses up the entire list. (When the entire list has been used up, first > last.) In the second case, quarry was not in the original list.

Step-by-Step Solution

Request Professional Solution

Request Solution!

We need at least 10 more requests to produce the solution.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the solution will be notified once they are available.
Add your Solution
Textbook Solutions and Answers Search
Solutions For Problems in Chapter 6
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT