![]() |
|
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||
Published: Wednesday, November 6, 2002 By Scott Mitchell
Introduction
Well, the answer depends largely upon whether or not the array is sorted. If the array is not sorted the best you can do is iterate through all of the items in the array, checking each element, until you either find the matching string or reach the end of the array. This approach is said to run in linear time since the amount of time required for the algorithm to run is linearly proportional to the length of the array. That is, if the array contains 1,000 elements, you will have to check at most 1,000 elements (assuming the element is in the last position of the array, or is not found at all in the array); if the array contains 1,000,000 elements, you will have to check at most 1,000,000 elements. Hence, there is a linear proportionality between the amount of work that needs to be done to search the array and the size of the array (namely, a proportion of 1).
However, if your array is sorted there is a much faster algorithm you can employ to determine whether or
not a particular element exists within the array, known as the binary search algorithm. In this article
we will examine how this algorithm works, its running time, and how to use the
Dissecting the Binary Search Algorithm A[0] = "are" A[1] = "few" A[2] = "girls" A[3] = "in" A[4] = "Missouri." A[5] = "Rolla," A[6] = "There" Now, say that you wanted to determine if the string "Rolla" existed in the array. If the array was not sorted you would have to iterate through all seven array elements, checking each to see if the value "Rolla" was found in the particular array element. With a sorted array, however, you can use the binary search algorithm, which performs the following steps:
The above steps are the nuts and bolts of the binary search algorithm. In plain English, the algorithm starts by checking the midpoint of the array. If, in sorted order, the element in the middle is appears after the element we're looking for then the element we're looking for must, intuitively, be somewhere between the start of the array and the midpoint, so we repeat our search using the midpoint of the beginning of the array and the midpoint of the array. If, however, the element at the array's midpoint appears before the element we're looking for, then we need only search the latter half of the array. If you're still having a bit of trouble wrapping your head around the binary search algorithm, consider how you might lookup a given word in the dictionary, say, "efficient"; assume that the dictionary has 800 pages. You know that "efficient", if it exists in the dictionary, must be located somewhere between page 0 and page 800, so you start by opening up the dictionary to page 400 (the middle of the current range) and checking to see what word was at the top of the page. Assume it was "neither". Since you know "efficient" comes before "neither" you know that "efficient" must be found somewhere between pages 0 and 400, so you turn to the middle of that range, page 200. Say the word you find there is "hibernate". Again, since "efficient" comes before "hibernate" you know that "efficient" must be found somewhere between pages 0 and 200, so you turn to page 100. There, you find the word "clammy"; hence, you know that "efficient" must be somewhere between page 100 and 200, so you turn to page 150... this would repeat until you find the word "efficient" or until your range reaches 1 page and "efficient" is not found (meaning it does not exist in the dictionary).
The Running Time of the Binary Search Algorithm Of course, to see the benefits of the binary search algorithm you must have your array in sorted order. Assume you have an unsorted array and you want to determine if an element exists. Would it make sense to sort the array and then use binary search? If you were planning on only using binary search once, then, no, it would not be smart, because the best sorting algorithms take time proportional to the size of the array times log2 of the size of the array. This means it would be quicker to use the iterative search method than to sort the array and then use the binary search method. Of course, if you planned on using binary search on the contents of a sorted array more than "log2 of the size of the array" times, then it would make sense to first sort the array and then use binary search.
Using the
The
This simple example starts with an unsorted array and then calls the
Now that we've seen how to use the
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
![]() |
![]() |
|||||||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||