Computer Science, asked by noeljohnson600, 1 month ago

write a python program To search an element in a list. (Linear Search).

Answers

Answered by sharmavinay84479
0

Answer:

Input : arr[] = {10, 20, 80, 30, 60, 50,

110, 100, 130, 170}

x = 110;

Output : 6

Element x is present at index 6

Input : arr[] = {10, 20, 80, 30, 60, 50,

110, 100, 130, 170}

x = 175;

Output : -1

Element x is not present in arr[].

Answered by anvisha27008
0

Answer: 01

I am new to python and we were given an assignment to create a linear search program that does not use "in" or index. The program compiles but says that every number I input is not in the list. I also have to do the same thing for a binary search but I'm doing things one at a time ha. Any help is appreciated!

PS: How could I show what index it is in without using the "index" function?

def linearSearch(intList,target):

   found = False

   position = 0

   while position < len(intList) and not found:

       if intList[position] == target:

           found = True

       position = position + 1

   return found

linearList = [3,5,9,7,6,12,15,9,1]

numInput = input("What number are you looking for? ")

numFound = linearSearch(linearList, numInput)

if numFound:

   print("The number is in index: ")

else:

   print("The number is not in the list")

Explanation:

Similar questions