linear search in python
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)))
linear search in python
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)))
Linear Search Algorithm python
def locate_card(cards, query):
# Create a variable position with the value 0
position = 0
# Set up a loop for repetition
while True:
# Check if element at the current position matche the query
if cards[position] == query:
# Answer found! Return and exit..
return position
# Increment the position
position += 1
# Check if we have reached the end of the array
if position == len(cards):
# Number not found, return -1
return -1
Linear Search Python
def linear_search(lst, target):
"""Returns the index position of the target if found, else returns -1"""
for i in range(0, len(lst)):
if lst[i] == target:
return i
return -1
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us