CPS120: Introduction to Computer Science Searching
Sequential Searching Although there are more efficient ways to search for data, sequential searching is a good choice of methods when amount of data to be searched is small. You simply check each element of an array position by position until you find the one that you are looking for. In any search, the item upon which the search is based is called the key and the field being searched is called the key field.
Binary Searching If the data is ordered (alphabetically, for example) in an array then a binary search is much more efficient than a sequential search. In the case of a binary search, you first examine the "middle" element. If that element is "lower" (alphabetically, for example) than the element that you are looking for, then you discard the lower half of the data and continue to search the upper half. The process repeats itself when you then look at the middle element of the remaining "upper half" of the data.
Binary Search A sequential search of a list begins at the beginning of the list and continues until the item is found or the entire list has been searched
Binary Search A binary search looks for an item in a list using a divide-and-conquer strategy Binary search algorithm assumes that the items in the list being searched are sorted The algorithm begins at the middle of the list in a binary search If the item for which we are searching is less than the item in the middle, we know that the item won’t be in the second half of the list Once again we examine the “middle” element (which is really the item 25% of the way into the list) The process continues with each comparison cutting in half the portion of the list where the item might be
Binary Search Trace of the binary search
Binary Search Average Number of Comparisons