Linear Search | Python3 program for linear search ; Run loop from 0 to right ; If search_element is found with left variable ; If search_element is found with right variable ; If element not found ; Driver code ; Function call
def search ( arr , search_Element ) : NEW_LINE INDENT left = 0 NEW_LINE length = len ( arr ) NEW_LINE position = - 1 NEW_LINE right = length - 1 NEW_LINE for left in range ( 0 , right , 1 ) : NEW_LINE INDENT if ( arr [ left ] == search_Element ) : NEW_LINE INDENT position = left NEW_LINE print ( " Element ▁ found ▁ in ▁ Array ▁ at ▁ " , position + 1 , " ▁ Position ▁ with ▁ " , left + 1 , " ▁ Attempt " ) NEW_LINE break NEW_LINE DEDENT if ( arr [ right ] == search_Element ) : NEW_LINE INDENT position = right NEW_LINE print ( " Element ▁ found ▁ in ▁ Array ▁ at ▁ " , position + 1 , " ▁ Position ▁ with ▁ " , length - right , " ▁ Attempt " ) NEW_LINE break NEW_LINE DEDENT left += 1 NEW_LINE right -= 1 NEW_LINE DEDENT if ( position == - 1 ) : NEW_LINE INDENT print ( " Not ▁ found ▁ in ▁ Array ▁ with ▁ " , left , " ▁ Attempt " ) NEW_LINE DEDENT DEDENT arr = [ 1 , 2 , 3 , 4 , 5 ] NEW_LINE search_element = 5 NEW_LINE search ( arr , search_element ) NEW_LINE