It is possible to take greater advantage of the ordered list if we areclever with our comparisons. In the sequential search, when we compareagainst the first item, there are at most \(n-1\) more items tolook through if the first item is not what we are looking for. Insteadof searching the list in sequence, a binary search will start byexamining the middle item. If that item is the one we are searching for,we are done. If it is not the correct item, we can use the orderednature of the list to eliminate half of the remaining items. If the itemwe are searching for is greater than the middle item, we know that theentire lower half of the list as well as the middle item can beeliminated from further consideration. The item, if it is in the list,must be in the upper half.
Hello Clever 3 13 18
2ff7e9595c
Comentários