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 searching in python #6

Merged
merged 3 commits into from Oct 8, 2022
Merged
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
36 changes: 36 additions & 0 deletions Searching/Linear-Search/in_python.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#linear searching in python
"""
Linear searching refers to sequential searching

starting from one end it searches for the desired element
till the pointer reaches the end of the array
"""
"""
* LINEAR SEARCH -
* Start searching from the first element till you find the element in an array
* and return the index.
*
* -if value doesnt exist then return -1.
* -max comparisons in the worst case will be 'N'(big-oh(N))
"""

def linear_search(array, N, target):

for i in range(0, N):
if target == array[i]:
return i
return -1

""" Examples:
"""
array={1,4,24,8,2}
N = len(array)
AnirudhDaya marked this conversation as resolved.
Show resolved Hide resolved
target=10
print(linear_search(array,N,target))

""" Output:
-1
"""