Average complexity: O(n) – This means when an element is present somewhere in the middle of the array.
Best case complexity: O(1) – This case occurs when the first element is the element to be searched.Worst-case complexity: O(n) – This case occurs when the search element is not present in the array.Here are the complexities of the linear search given below.Īs linear search algorithm does not use any extra space, thus its space complexity = O(n) for an array of n number of elements. Step 5: Hence ITEM = ARR thus LOC updated to 5. Step 4: ITEM != 7 thus we move to the next element. Step 3: ITEM != 14 thus we move to next element. Step 2: ITEM != 56 thus we move to next element. Step 1: ITEM != 77 thus we move to next element. And we need to find whether ITEM= 18 is present in this array or not. Let’s say, below is the ARR with 10 elements. LSEARCH(ARR, N, ITEM, LOC) Here ARR Is the array of N number of elements, ITEM holds the value we need to search in the array and algorithm returns LOC, the location where ITEM is present in the ARR. While comparing ITEM with data at each ARR location, and once ITEM = ARR, LOC is updated with location N+1. For this, LOC is assigned to -1, which indicates that ITEM is not present in ARR.
Suppose ARR is an array of n elements, and we need to find location LOC of element ITEM in ARR. This is a straightforward and basic algorithm. One example of such an algorithm is a linear search. In this type of search, all the elements of the list are traversed one by one to find if the element is present in the list or not. This is the traditional technique for searching an element in a collection of elements. Hadoop, Data Science, Statistics & others 1.