Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

linear_Search.py #591

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions Algorithms/Python/linear_Search.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#python code for linear search
MAX = 20
#array of items on which linear search will be conducted.
intArray = [1,2,3,4,6,7,9,11,12,14,15,16,17,19,33,34,43,45,55,66]
def printline(count):
for i in range(count-1):
print("=", end="")
print("=")
#this method makes a linear search.
def find(data):
comparisons = 0
index = -1
#navigate through all items
for i in range(MAX):
#count the comparisons made
comparisons += 1

#if data found, break the loop
if data == intArray[i]:
index = i
break
print("Total comparisons made:", comparisons)
return index
def display():
print("[", end="")
#navigate through all items
for i in range(MAX):
print(intArray[i], end=" ")
print("]")
print("Input Array: ", end="")
display()
printline(50)
#find location of 1
location = find(55)
#if element was found
if location != -1:
print("\nElement found at location:", location+1)
else:
print("Element not found.")