Skip to content

Commit

Permalink
Merge pull request #148 from urmil22/optimize-linear-search
Browse files Browse the repository at this point in the history
optimize linear search
  • Loading branch information
Anjan50 authored Oct 29, 2023
2 parents 9c9f721 + 267e940 commit 61f4e2f
Showing 1 changed file with 6 additions and 9 deletions.
15 changes: 6 additions & 9 deletions Searching/linear_search.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
def search(arr, n, x):

for i in range(0, n):
if (arr[i] == x):
return i
def search(arr, x):
for index, element in enumerate(arr):
if element == x:
return index
return -1


# Driver Code
arr = [2, 3, 4, 10, 40]
x = 10
n = len(arr)
result = search(arr, n, x)
if (result == -1):
result = search(arr, x)
if result == -1:
print("Element is not present in array")
else:
print("Element is present at index", result)

0 comments on commit 61f4e2f

Please sign in to comment.