only one comparison is enough. Similarly, the worst case in this scenario would be if the complete list is searched and the element is found only at the end of the list or is not found in the list. The efficiency of an algorithm in that case would be expressed as O(n) because n comparisons required to complete the search. The average case efficiency of an algorithm can be obtained by finding the average number of comparisons as given below: Minimum number of comparisons = Maximum number of comparisons = n If the element not found then maximum number of comparison = n Therefore, average number of comparisons = (n + )/ Hence the average case efficiency will be expressed as O (n).
Algorithm for Searching Techniques . . . Linear Search Linear search also called sequential search is a sequential method for finding a particular value in a list.
This method checks the search element with each element in sequence until the desired element is found or the list is exhausted. In this searching algorithm, list need not be ordered. Procedure . Traverse the array using for loop .
In every iteration, compare the target search key value with the current value of the list. • If the values match, display the current index and value of the array • If the values do not match, move on to the next array element. . If no match is found, display the search element not found.
To search the number in the array given below, linear search will go step by step in a sequential order starting from the first element in the given array if the search element is found that index is returned otherwise the search is continued till the last index of the array. In this example number is found at index number . 12th Computer Chapter - - Algorithmic Strategies index values Input: values[] = { , , , , , } target = Output: Example : Input: values[]