6.2 Linear SearchThe simplest solution to the sequence search problem is the sequential or linear search algorithm. This technique iterates over the sequence, one item at a time, until the specific item is found or all items have been examined. In Python, a target item can be found in a sequence using the if key in theArray : print("The key is in the array.") else : print("The key is not in the array.") The use of the Finding a Specific ItemThe function in the source listing below implements the sequential search algorithm, which results in a boolean value indicating success or failure of the search. Program Listing
This is the same operation performed by the Python linear search worst-case:
To analyze the sequential search algorithm for the worst case, we must first determine what conditions constitute the worst case. Remember, the worst case occurs when the algorithm performs the maximum number of steps. For a sequential search, that occurs when the target item is not in the sequence and the loop iterates over the entire sequence. Assuming the sequence contains items, the linear search has a worst case time of . Question 6.2.1
Does the linear search have a best case? If so, when does the best case occur? When does the worst case occur? Yes.
Searching a Sorted SequenceA linear search can also be performed on a sorted sequence, which is a sequence containing values in a specific order. For example, the values in the array illustrated in Figure 6.2.2 are in ascending or increasing numerical order. That is, each value in the array is larger than its predecessor. A linear search on a sorted sequence works in the same fashion as that for the unsorted sequence, with one exception. It's possible to terminate the search early when the value is not in the sequence instead of always having to perform a complete traversal. For example, suppose we want to search for 8 in the array from Figure 6.2.2. When the fourth item, which is value 10, is examined, we know value 8 cannot be in the sorted sequence or it would come before 10. The implementation of a linear search on a sorted sequence is provided below. Program Listing
The only modification to the earlier version is the inclusion of a test to determine if the current item within the sequence is larger than the target value. If a larger value is encountered, the loop terminates and Question 6.2.2
What is the worst case time-complexity for the
Select the correct answer below by clicking on the appropriate box.
The worst case time-complexity for the linear search is still linear even though it is applied to a sorted sequence. In the worst case, it must still examine every element of the sequence.
Finding the Smallest ValueInstead of searching for a specific value in an unsorted sequence, suppose we wanted to search for the smallest value, which is equivalent to applying Python's Program Listing
To prime the loop, we assume the first value in the sequence is the smallest and start the comparisons at the second item. Since the smallest value can occur anywhere in the sequence, we must always perform a complete traversal, resulting in a worst case time of . Question 6.2.3
Does the Since the smallest value can occur anywhere within the sequence, we must always perform a complete traversal. Thus, searching for the smallest value within in a sequence does not have a best case.
|