When data items are stored in a arsenal such together a list, we say thatthey have a direct or sequential relationship. Every data items is storedin a place relative to the others. In Python lists, these relativepositions room the index values of the separation, personal, instance items. Since theseindex values space ordered, that is possible for us to visit lock insequence. This process gives rise to our very first searching technique, thesequential search.

You are watching: The average-case time efficiency of a sequential search on an array is

Figure 1 shows just how this find works. Starting at the firstitem in the list, we simply move indigenous item come item, complying with theunderlying sequential ordering until we either find what we room lookingfor or run out the items. If we run out the items, we have discovered thatthe item we were trying to find was no present.

Figure 1: Sequential search of a perform of Integers¶

The Python implementation for this algorithm is presented inCodeLens 1. The duty needs the list and also the item weare feather for and returns a boolean value regarding whether it is present.The boolean variable uncovered is initialized to False and isassigned the worth True if we uncover the article in the list.

Activity: CodeLens Sequential find of an Unordered list (search1)

## 6.3.1. Evaluation of Sequential Search¶

To analyze looking algorithms, we have to decide ~ above a an easy unit ofcomputation. Recall that this is commonly the typical step that must berepeated in bespeak to fix the problem. Because that searching, it makes sense tocount the variety of comparisons performed. Every comparison might or maynot find the item we are looking for. In addition, we make anotherassumption here. The perform of item is not ordered in any kind of way. The itemshave been inserted randomly into the list. In various other words, the probabilitythat the item we are in search of is in any certain position isexactly the exact same for each place of the list.

If the item is not in the list, the only method to understand it is to compare itagainst every item present. If there space (n) items, climate thesequential search needs (n) compare to uncover that theitem is not there. In the instance where the article is in the list, theanalysis is no so straightforward. There are actually three differentscenarios that have the right to occur. In the best case us will discover the article in thefirst place we look, in ~ the beginning of the list. We will need only onecomparison. In the worst case, we will certainly not find the item until thevery last comparison, the nth comparison.

What about the typical case? on average, us will discover the items abouthalfway right into the list; the is, we will compare against(fracn2) items. Recall, however, that as n it s okay large,the coefficients, no issue what lock are, come to be insignificant in ourapproximation, for this reason the complexity of the sequential search, is(O(n)). Table 1 summarizes this results.

Table 1: Comparisons provided in a Sequential search of an Unordered List

Case

Best Case

Worst Case

Average Case

item is present

(1)

(n)

(fracn2)

item is no present

(n)

(n)

(n)

We assumed earlier that the item in our collection had been randomlyplaced so the there is no relative order between the items. What wouldhappen to the sequential search if the items to be ordered in some way?Would we be able to gain any kind of efficiency in our search technique?

Assume the the list of items was created so that the items to be inascending order, from short to high. If the item we are trying to find ispresent in the list, the opportunity of it being in any type of one of the npositions is still the very same as before. We will still have the samenumber of to compare to uncover the item. However, if the items is notpresent over there is a slight advantage. Figure 2 shows thisprocess together the algorithm looks for the item 50. Notification that item arestill compared in sequence till 54. In ~ this point, however, us knowsomething extra. Not just is 54 no the items we are looking for, yet noother facets beyond 54 deserve to work either because the list is sorted. Inthis case, the algorithm go not have actually to proceed looking v allof the items come report that the article was not found. It deserve to stopimmediately. CodeLens 2 mirrors this sports of thesequential find function.

See more: My Cat Bites, Then Licks Me: Why Does My Cat Lick And Then Bite Me

Figure 2: Sequential find of an Ordered list of Integers¶

Table 2 summarizes this results. Keep in mind that in the bestcase we could discover the the item is not in the list by looking atonly one item. On average, we will understand after looking through only(frac n2) items. However, this an approach is still(O(n)). In summary, a sequential search is improved by orderingthe list just in the situation where we carry out not find the item.

Table 2: Comparisons provided in Sequential search of an bespeak List

item is present

(1)

(n)

(fracn2)

item not present

(1)

(n)

(fracn2)

Q-3: intend you space doing a sequential search of the perform <15, 18, 2, 19, 18, 0, 8, 14, 19, 14>. How plenty of comparisons would certainly you have to do in stimulate to uncover the vital 18?

5Five comparisons would acquire the 2nd 18 in the list. 10You perform not should search the whole list, just until you find the an essential you space looking for. 4No, psychic in a sequential find you begin at the beginning and check each an essential until you uncover what you are in search of or exhaust the list. 2In this case only 2 compare were essential to discover the key.

Q-4: expect you space doing a sequential find of the ordered perform <3, 5, 6, 8, 11, 12, 14, 15, 17, 18>. How plenty of comparisons would you need to do in order to find the an essential 13?

10You perform not must search the whole list, since it is bespeak you have the right to stop searching as soon as you have compared with a value larger than the key. 5Since 11 is less than the key value 13 you must keep searching. 7Since 14 is better than the vital value 13 you can stop. 6Because 12 is much less than the an essential value 13 you need to keep going.