Skip to content

Commit 524882c

Browse files
authored
Merge pull request #591 from SaraswatAnushka/master
linear_Search.py
2 parents f885893 + 6ac8214 commit 524882c

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

Algorithms/Python/linear_Search.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#python code for linear search
2+
MAX = 20
3+
#array of items on which linear search will be conducted.
4+
intArray = [1,2,3,4,6,7,9,11,12,14,15,16,17,19,33,34,43,45,55,66]
5+
def printline(count):
6+
for i in range(count-1):
7+
print("=", end="")
8+
print("=")
9+
#this method makes a linear search.
10+
def find(data):
11+
comparisons = 0
12+
index = -1
13+
#navigate through all items
14+
for i in range(MAX):
15+
#count the comparisons made
16+
comparisons += 1
17+
18+
#if data found, break the loop
19+
if data == intArray[i]:
20+
index = i
21+
break
22+
print("Total comparisons made:", comparisons)
23+
return index
24+
def display():
25+
print("[", end="")
26+
#navigate through all items
27+
for i in range(MAX):
28+
print(intArray[i], end=" ")
29+
print("]")
30+
print("Input Array: ", end="")
31+
display()
32+
printline(50)
33+
#find location of 1
34+
location = find(55)
35+
#if element was found
36+
if location != -1:
37+
print("\nElement found at location:", location+1)
38+
else:
39+
print("Element not found.")

0 commit comments

Comments
 (0)