

Halfway into the list that is, we will compare against n 2 \frac 2 n items. What about the average case? On average, we will find the item about The worst case, we will not discover the item until the very last In the best case we will find the item in the first place we There are actually three different scenarios that can In the case where the item is in the list, the analysis is not so Search requires n n n comparisons to discover that the item is not there. If there are n n n items, then the sequential If the item is not in the list, the only way to know it is to compare itĪgainst every item present. That the item we are looking for is in any particular position isĮxactly the same for each position of the list. The list of items is not ordered in any way. In addition, we make anotherĪssumption here. Not discover the item we are looking for. For searching, it makes sense toĬount the number of comparisons performed. Recall that this is typically the common step that must be To analyze searching algorithms, we need to decide on a basic unit ofĬomputation. Sequential_search (testlist, 13 ) # => True Analysis of Sequential Search Remember in practice we would use the Python in operator for this purpose, so you can think of the below algorithm as what we would do if in were not provided for us. The item we are looking for and returns a boolean value as to whether it The Python implementation for this algorithm is shown below. If we run out of items, we haveĭiscovered that the item we were searching for was not present. The underlying sequential ordering until we either find what we are The first item in the list, we simply move from item to item, following The diagram below shows how this search works. This process gives rise to our first searching technique, the Index values are ordered, it is possible for us to visit them in Positions are the index values of the individual items. They have a linear or sequential relationship.

Step 6 - If last element in the list also doesn't match, then display "Element is not found!!!" and terminate the function.Ĭonsider the following list of elements and the element to be searched.When data items are stored in a collection such as a list, we say that.Step 5 - Repeat steps 3 and 4 until search element is compared with last element in the list.Step 4 - If both are not matched, then compare search element with the next element in the list.

