Binary search number of comparisons

Binary search number of comparisons

Posted: saccubi@mail.ru Date of post: 12.06.2017

Binary search is another approach to the search problem. It is much more efficient than sequential search, but requires that the search list be presorted. Essentially, binary search begins by jumping to the midpoint of the search list and comparing the item it finds there to the target item it is searching for. Consider the result of this comparison.

Since the search list was presorted, if the target item occurs anywhere in the list, it must be after the midpoint. Hence, the first half of the list — everything up to and including the middle item — can be safely discarded without an exhaustive examination of each of the items in that portion of the list.

If the middle item had been larger than the target, then the target could only occur in the first half of the list. The second half of the list — the middle item and everything after it — could be dropped.

Watson

After performing this first step, the current list would be half the size of the original list. But, what should be done next? The answer is to repeat this process on the new list: This process may be repeated until either: The binary search algorithm is presented formally in.

Binary search depends on being able to quickly determine which item is in the middle of the list of remaining items. If the number of items in the list is odd, then there is a well-defined middle item. If the list is composed of an even number of items, then there is no item in the exact middle of the list. Note that in either case, only three comparisons were required to determine whether the target item was in the original seven item list.

binary search number of comparisons

In fact, no more than three comparisons will ever be needed in a binary search of a seven item list. A sequential search of a seven item list requires 3.

Before a comparison between the target item and a list item can be performed, the list item must be selected.

In sequential search this amounted to simply selecting the next item in the list adding one to the current position. So, in reality, binary search might actually take longer than sequential search on a list of seven items. The true advantage of binary search over sequential search can really only be seen on large problem sizes. So, how does binary search compare to sequential search?

As stated earlier, a sequential search of a 10, item list requires, on average 5, comparisons to find an item that is in the list, and 10, comparisons to determine that an item is not present in the list.

Binary Options Trading with BinaryMate

A binary search of 10, items requires at most 14 comparisons. Thus, in terms of the number of comparisons, binary search is much more efficient than sequential search. However, in order to use the binary search approach, the items must be presorted.

The actual amount of time required by any search algorithm depends on both the number of comparisons it performs and the amount of time needed to perform each comparison. A comparison operation in binary search may take longer than a comparison operation in sequential search since the location of the middle item must be determined.

In reality, a binary search comparison does not take anywhere near that long, but in the following analysis — which will show binary search to be more efficient than sequential search — we want to err on the side of caution. Number of comparisons in a binary search of a 10, item list worst case. Given that at most 14 binary search comparisons are required to search 10, items, the search will take no more than 1.

When we consider larger lists, such as a search through million social security records, the performance of binary search becomes even more impressive. This amazing result follows from the fact that each comparison eliminates one half of the items that remain to be searched. A comparison of the predicted runtimes of sequential and binary search. The maximum number of comparisons required to perform a binary search of N items can be expressed mathematically as:.

You should convince yourself that this formula is correct by working through the binary search algorithm on a number of small examples in order to establish how many comparisons are actually needed, and then check that number with the formula.

Note that the formula even works in the case of a list with zero items. Using this equation, it is possible to predict that a binary search of a 1, item list will require, at most 10 comparisons. The runtime of sequential search appears as a straight line with a constant slope. For this reason, sequential search is known as a linear algorithm.

Binary search appears as a curve with a slope that grows ever more shallow with increasing problem size, and is known as a logarithmic algorithm. In general, logarithmic algorithms are preferred over linear algorithms, since they tend to run faster on large problems.

In spite of the fact that we assumed a comparison would require times as long in binary search as in sequential search, the binary search algorithm is still clearly more efficient for searching large lists. One way of understanding why this is true is to realize that the size of the list must double before binary search performs a single additional comparison. So, as problem size increases, the initial advantage that sequential search had due to its faster comparison time is quickly overcome by the smaller number of comparisons required by binary search.

Under the assumptions of , binary search becomes faster than sequential search on lists that contain about 2, or more items. Under different assumptions, the point at which binary search becomes faster than sequential search would shift. For example, if we assumed that binary search comparisons were ten times slower than sequential search comparisons, binary search would become faster than sequential search on lists that contained more than items.

Finally, it is important to note, once again, that binary searches can only be performed on lists that are already sorted. Given an unsorted list, it is necessary to first sort the list before beginning the binary search procedure. This observation leads us to our next topic of discussion: However, when computing an average, fractions are often produced. If the list is empty has no items then 1. Let the current item be the item in the middle of the list 3.

Error (Forbidden)

If the current item is the same as the target item then 3. If the current item is larger than the target item then repeat this entire procedure on the first half of the list -- the items up to, but not including, the middle item 5.

A comparison of the predicted runtimes of sequential and binary search The maximum number of comparisons required to perform a binary search of N items can be expressed mathematically as: Exercises for In a manner similar to , illustrate the behavior of a binary search for the value 75 in the list: Using the same list given in problem 1, supply a target value a value to be searched for that could be found by binary search using less than the maximum number of comparisons predicted for this list.

What is the exact number of comparisons needed to find the target you supplied? What is the maximum number of comparisons needed to perform a binary search of a 63 item list?

An 80 item list? What is the largest list that can be searched by the binary search method given 15 or fewer comparisons? A 2, item list? A 10, item list?

inserted by FC2 system