forked from thekai112/DSA-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinear_Search.py
More file actions
40 lines (33 loc) · 1.02 KB
/
Linear_Search.py
File metadata and controls
40 lines (33 loc) · 1.02 KB
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
def LinearNRSearch(alist, i):
found = False
for j in range(len(alist)):
if alist[j] == i:
found = True
break
return print(f"Element {i} found at index {j}") if found else print(f"Element {i} not found")
def LinearRS(alist, k):
if len(alist) == 0:
return False
else:
return LinearRS(alist[1:], k) or alist[0] == k
array = []
print("Enter the number of elements in the array: ")
n = int(input())
print("Enter the elements of the array: ")
for i in range(n):
print(f"Enter The {i + 1}th element: ")
array.append(int(input()))
print("How do you want to search the array?")
print("1. Recursive Search")
print("2. Non Recursive Search")
choice = int(input())
if choice == 1:
print("Enter the element to be searched: ")
item = int(input())
print(f"Element found @ index {LinearRS(array, item)}")
elif choice == 2:
print("Enter the element to be searched: ")
item = int(input())
LinearNRSearch(array, item)
else:
print("Invalid Choice")