-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinear_search.py
66 lines (55 loc) · 1.44 KB
/
linear_search.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#import decimal
import random
print("\n\t<< LINEAR SEARCH ALGORITHM >>\n\n")
print("You can store elements with following options :\
\n1)random static //machine generated random array \n2)custom // create by your own brain")
op=int(input())
i=0
fno=0
search_array=[]
def linear_search(n,fno):
while(i<n):
j=search_array[i]
print("j:",j)
print("i",i)
print("n",n)
print("fno",fno)
if j == fno:
print("matched")
print("Your number is located on :",i+1)
break
else:
print("not matched")
return "Your number is not in list"
if op==1:
print("random elements are:")
print("How many elements you want to store ?")
n=int(input())
print("\n\nYOUR ",n," RANDOM NUMBER WILL GENERATE IN RANGE OF <1 TO 100000>\n")
i=0
for i in range(n):
search_array.insert(i,random.randrange(100000))
print("Your array of ",n," is :\n",search_array)
print("Enter number you are looking for")
fno=int(input())
#ranno=random.randrange(0,ranno)
#print("rano",ranno)
print(linear_search(n,fno))
elif op==2:
#search_array=[]
print("custom elements are :")
print("How many elements you want to store in array")
n=int(input())
print("please provide ",n," elements to store")
i=0
for i in range(n):
j=0
j=int(input())
search_array.insert(i,j)
print("array: ",search_array)
print("Enter number you want to search")
fno=int(input())
print(linear_search(n,fno))
else:
print("Invalid input !! please provide proper input")
#print(search_array)