def linearsearch(arr, x): for i in range(len(arr)): if arr[i] == x: return i return -1 arr = [1,2,3,4,5,6,7,8] x = 4 print("element found at index "+str(linearsearch(arr,x)))
Here is what the above code is Doing:
1. We have an array of integers.
2. We have an integer x.
3. We are checking if x is present in the array.
4. If it is present, we are returning the index of the element.
5. If it is not present, we are returning -1.